Structure name for string

How to print the name of the struct type, i.e. i can include it in the print statement i.e. something like

type MyStruct struct { ... }

func main() {
    fmt.Println(MyStruct.className())
}

      

If possible, can it be considered slow? (i.e. reflection)

+3


source to share


1 answer


For example,

package main

import "fmt"

type MyStruct struct{}

func main() {
    fmt.Printf("%T\n", MyStruct{})
}

      

Output:



main.MyStruct

      

fmt

%T

print verb gives the syntactic representation of the type of the type.

The Go fmt

package uses the package reflect

for runtime reflection.

+8


source







All Articles