Home  >  Article  >  Backend Development  >  Let’s talk about the conversion between golang () and .()

Let’s talk about the conversion between golang () and .()

PHPz
PHPzOriginal
2023-03-30 10:32:001033browse

() and .() conversion in Golang

In Golang, () and .() are two very common type conversions. In this article, we will introduce the usage scenarios, conversion rules and related sample codes of these two type conversions.

() conversion

In Golang, () conversion is the process of converting a non-interface type into an interface type. This process is accomplished by encapsulating a value in a new interface type. The syntax of () conversion is as follows:

var i interface{} = somevalue

In the above code, we convert a value somevalue to an interface type and store it in a variable imiddle. In this process, the Golang compiler will create a new interface type for i.

() conversion can be applied to any type, including custom types and built-in types. When using () conversion, we can convert any non-interface type to an interface type. In this way, we can use the methods of the interface to operate on these types of values.

Next, let’s look at a sample code. Suppose we define an interface type named shape. This interface contains a area() method for calculating the area of ​​a shape. We also define a custom type named rect to represent a rectangle. We can convert the rect type to the shape type through () conversion, and then use the area() method to calculate the area of ​​the rectangle. The sample code is as follows:

package main

import "fmt"

type shape interface {
  area() float64
}

type rect struct {
  width, height float64
}

func (r rect) area() float64 {
  return r.width * r.height
}

func main() {
  r := rect{width: 3, height: 4}
  s := shape(r) // 将rect类型转换为shape类型
  fmt.Println("Area of rectangle:", s.area())
}

Run the above code, we will get the following output:

Area of rectangle: 12

As can be seen from the above sample code, we convert a rect by using () The type value is converted to a shape type value, and then the area() method is used to calculate the area of ​​the rectangle.

.() conversion

In Golang, .() conversion is the process of converting an interface type to a non-interface type. This process is accomplished by extracting the value from the interface type and converting it to a new non-interface type. The syntax of .() conversion is as follows:

var aType someType = i.(someType)

In the above code, we use .() conversion to convert an interface type i to a non-interface typesomeType. During this process, Golang will check whether the actual type of the value stored in the interface type matches someType. If the match is successful, Golang will extract the value stored in the interface type and convert it into the someType type and store it in the variable aType. If the match fails, the program will throw a panic at runtime.

When using .go() conversion, we must ensure that the actual type of the value stored in the interface type matches the non-interface type we want to convert to, otherwise the program will throw a panic at runtime.

Next, let’s look at a sample code. Suppose we have an interface type named shape. This interface contains a area() method for calculating the area of ​​a shape. We also define a custom type named rect to represent a rectangle. We can convert the shape type to the rect type through .go() conversion and calculate the area of ​​the rectangle. The sample code is as follows:

package main

import "fmt"

type shape interface {
  area() float64
}

type rect struct {
  width, height float64
}

func (r rect) area() float64 {
  return r.width * r.height
}

func main() {
  s := shape(rect{width: 3, height: 4}) // 将rect类型转换为shape类型
  r := s.(rect)                         // 将shape类型转换为rect类型
  fmt.Println("Area of rectangle:", r.area())
}

Run the above code, we will get the following output:

Area of rectangle: 12

As can be seen from the above sample code, we convert a rect by using () Convert a value of type to a value of type shape, and convert it back to a value of type rect using .go() conversion, then use area() The method calculates the area of ​​this rectangle.

Summary

In this article, we introduced the () and .() conversions in Golang, including their usage scenarios, conversion rules and related sample codes. Both of these type conversions are very common type conversions in Golang, and any Golang developer should have a deep understanding of them.

The above is the detailed content of Let’s talk about the conversion between golang () and .(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn