The go.types package is a package library that provides operations on the underlying types in Go language. This package allows the identification of type and kind of any Go type, which is very useful in many scenarios such as variable declarations, function signatures, or marshalling and unmarshalling of data structures.
One of the types provided by go.types is “TypeUnderlying”. This type enables the determination of the underlying type of any given type.
Here are a few examples of the “TypeUnderlying” type in action:
Example 1:
package main
import ( "fmt" "reflect" )
type customType []string
func main() { v := customType{"one", "two", "three"} t := reflect.TypeOf(v)
under := t.(reflect.TypeUnderlying) fmt.Println(under.Underlying()) }
Output: []string
In this example, we have defined a custom type called “customType” and instantiated a variable “v” with this type. We then use the reflect package to obtain the reflect.Type instance of variable “v”. We then use the “Underlying()” function of the “TypeUnderlying” type to determine the underlying type of our customType, which prints “[]string” to the console.
Example 2:
package main
import ( "fmt" "reflect" )
func main() { num := 101 t := reflect.TypeOf(num)
under := t.(reflect.TypeUnderlying) fmt.Println(under.Underlying()) }
Output: int
In this example, we have instantiated a variable “num” with a type of “int” and obtain its reflect.Type instance using the reflect package. We then use the “Underlying()” function of the “TypeUnderlying” type to determine the underlying type of “int”, which prints “int” to the console.
Golang Type.Underlying - 30 examples found. These are the top rated real world Golang examples of go/types.Type.Underlying extracted from open source projects. You can rate examples to help us improve the quality of examples.