Print the Type of variable from struct

I am trying to store the type of variable in a struct and print the value of it.
code:-

package main

import (
	"fmt"
	"reflect"
)

type a struct {
	typ reflect.Type
	s   string
}

func main() {
	ad := &a{
		s: "akhil",
	}
	ad.typ = ak()
	fmt.Println(ad)
}

func ak() reflect.Type {
	return reflect.TypeOf(int32(0))
}

OUTPUT:
&{0x49b360 akihl}

In place of address or something how to print the type.

Thanks.

You can print that field fmt.Println(ad.typ). But if you want print struct and show type that hold in value of typ field you need to implement String method for a struct type.

func (v a) String() string {
	return fmt.Sprintf("{%v %v}", v.typ, v.s)
}

For ex if my struct has 10 values than the i have print all ten values ?

That is difficult.

type ColumnType struct {
	name string

	hasNullable       bool
	hasLength         bool
	hasPrecisionScale bool

	nullable     bool
	length       int64
	databaseType string
	precision    int64
	scale        int64
	scanType     reflect.Type
}

This is a struct in sql package. I have stored the type in scanType field. so when i am printing it it print the address.

{name:A hasNullable:true hasLength:true hasPrecisionScale:true nullable:true length:12 databaseType:DECIMAL precision:10
scale:2 scanType:0x4c5100}


{name:B hasNullable:true hasLength:true hasPrecisionScale:true nullable:true length:7 databaseType:DECIMAL precision:5 scale:2 scanType:0x4c5100} 

If i use (variable.scanType) in print it will show us that no that type of field or method because it is not exported.

How to solve this is?

Thanks

I think you can use this package https://github.com/davecgh/go-spew/ alternative to fmt.Println for debugging deep nested struct. I’v tried and it can inspect the value of type reflect.Type.

From go itself we cant print it ?.

Yes I think no function from standard package can do like that.

Ok…Thanks.

I am not quite sure what you want to do. But you can use the Stringer interface to define how your types should be represented.

package main

import (
	"fmt"
)

type Outer struct {
	A InnerA
	B InnerB
}

func (o Outer) String() string {
	return fmt.Sprintf("Outer(%s, %s)", o.A, o.B)
}

type InnerA struct {
	S string
}

func (a InnerA) String() string {
	return fmt.Sprintf("A(S=\"%s\")", a.S)
}

type InnerB struct {
	S string
}

func (b InnerB) String() string {
	return fmt.Sprintf("B(S=\"%s\")", b.S)
}

func main() {
	o := Outer{A: InnerA{S: "in A"}, B: InnerB{S: "in B"}}
	fmt.Printf("using Stringer: %s\n", o)
}

Output:

using Stringer: Outer(A(S="in A"), B(S="in B"))

https://play.golang.com/p/fJlafDbbyFw

@lutzhorn Sorry, I didn’t get you. can u explain me with an example with printing the types of a variable.

Thanks.

Use %T to print the type of a value.

package main

import (
	"fmt"
)

type Outer struct {
	A InnerA
	B InnerB
}

func (o Outer) String() string {
	return fmt.Sprintf("%T(%s, %s)", o, o.A, o.B)
}

type InnerA struct {
	S string
}

func (a InnerA) String() string {
	return fmt.Sprintf("%T(%T(\"%s\"))", a, a.S, a.S)
}

type InnerB struct {
	S string
}

func (b InnerB) String() string {
	return fmt.Sprintf("%T(%T(\"%s\"))", b, b.S, b.S)
}

func main() {
	o := Outer{A: InnerA{S: "in A"}, B: InnerB{S: "in B"}}
	fmt.Printf("using Stringer: %s\n", o)
}

Output:

using Stringer: main.Outer(main.InnerA(string("in A")), main.InnerB(string("in B")))

https://play.golang.com/p/nwpyc5aE3s3

Thanks @lutzhorn

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.