Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the type of an object in Go?

Tags:

go

go-reflect

How do I find the type of an object in Go? In Python, I just use typeof to fetch the type of object. Similarly in Go, is there a way to implement the same ?

Here is the container from which I am iterating:

for e := dlist.Front(); e != nil; e = e.Next() {     lines := e.Value     fmt.Printf(reflect.TypeOf(lines)) } 

I am not able to get the type of the object lines in this case which is an array of strings.

like image 759
Rahul Avatar asked Nov 24 '13 02:11

Rahul


People also ask

What is type keyword in Golang?

The type keyword is there to create a new type. This is called type definition. The new type (in your case, Vertex) will have the same structure as the underlying type (the struct with X and Y). That line is basically saying "create a type called Vertex based on a struct of X int and Y int".

What is interface type in Golang?

Interface Types: The interface is of two types one is static and another one is dynamic type. The static type is the interface itself, for example, tank in the below example. But interface does not have a static value so it always points to the dynamic values.

How do you declare an object in Go?

Using the New Keyword to Create Objects Another way to create an object from a struct is to use a new keyword. While using a new keyword, Golang creates a new object of the type struct and returns its memory location back to the variable. In short, the new keyword returns the address of the object.

How do you implement an interface in Go?

Implementing an interface in Go To implement an interface, you just need to implement all the methods declared in the interface. Unlike other languages like Java, you don't need to explicitly specify that a type implements an interface using something like an implements keyword.


2 Answers

The Go reflection package has methods for inspecting the type of variables.

The following snippet will print out the reflection type of a string, integer and float.

package main  import (     "fmt"     "reflect" )  func main() {      tst := "string"     tst2 := 10     tst3 := 1.2      fmt.Println(reflect.TypeOf(tst))     fmt.Println(reflect.TypeOf(tst2))     fmt.Println(reflect.TypeOf(tst3))  } 

Output:

Hello, playground string int float64 

see: http://play.golang.org/p/XQMcUVsOja to view it in action.

More documentation here: http://golang.org/pkg/reflect/#Type

like image 52
dethtron5000 Avatar answered Sep 28 '22 16:09

dethtron5000


I found 3 ways to return a variable's type at runtime:

Using string formatting

func typeof(v interface{}) string {     return fmt.Sprintf("%T", v) } 

Using reflect package

func typeof(v interface{}) string {     return reflect.TypeOf(v).String() } 

Using type assertions

func typeof(v interface{}) string {     switch v.(type) {     case int:         return "int"     case float64:         return "float64"     //... etc     default:         return "unknown"     } } 

Every method has a different best use case:

  • string formatting - short and low footprint (not necessary to import reflect package)

  • reflect package - when need more details about the type we have access to the full reflection capabilities

  • type assertions - allows grouping types, for example recognize all int32, int64, uint32, uint64 types as "int"

like image 36
Grzegorz Luczywo Avatar answered Sep 28 '22 14:09

Grzegorz Luczywo