where is go code generate type info?

143 views
Skip to first unread message

xie cui

unread,
Aug 13, 2020, 9:52:44 AM8/13/20
to golang-nuts
package main

import (
"fmt"
"reflect"
)

type Foo struct {
A int
}

func (f *Foo) Test() {

}
func main() {
f := &Foo{}
t := reflect.TypeOf(f)
fmt.Println(t)
}

when call reflect.TypeOf can get a struct desc the type, i know this type struct in generate by compiler, and i need to know where is this code, and how to generate the struct fields and methods?

Jan Mercl

unread,
Aug 13, 2020, 10:47:28 AM8/13/20
to xie cui, golang-nuts
On Thu, Aug 13, 2020 at 3:53 PM xie cui <cuiw...@gmail.com> wrote:

> ..., i know this type struct in generate by compiler, and i need to know where is this code, and how to generate the struct fields and methods?

To avoid the possibility of the XY problem, can you please tell more
about the goal of "generating struct fields and methods" and what
exactly is meant by that? Some things can be done by the compiler,
some in user code and the two feature sets are definitely not the
same.

xie cui

unread,
Aug 13, 2020, 10:44:57 PM8/13/20
to golang-nuts

the return of reflect.TypeOf should be generate by compile, i am trying to understand it, so i need to know where is code generate it in compiler. i need to know compiler parts. and i am curious about what is do in user code also.  please tell me about it. i will be appriciated.

Volker Dobler

unread,
Aug 14, 2020, 1:32:25 AM8/14/20
to golang-nuts
The value returned by reflect.TypeOf is not computed during
compile time but during run time. The code for package
reflect is generated by the compiler during compile time
but this is not interesting to understand reflection at all as
it is the same code generation like for lets say net/http.

The source code for package reflect is open source, just
links directly to the source code.

Understanding of reflection does not happen by studing
the compiler and not the implementation of package reflect
but by reading the appropriate blog post on blog.golang.org.

V.

xie cui

unread,
Aug 14, 2020, 2:15:03 AM8/14/20
to golang-nuts

how can i see the code genrate by compiler, and where is the compiler code to generate these code(which file, which function). i am just curious.

Volker Dobler

unread,
Aug 14, 2020, 5:08:30 AM8/14/20
to golang-nuts
On Friday, 14 August 2020 08:15:03 UTC+2, xie cui wrote:

how can i see the code genrate by compiler, and where is the compiler code to generate these code(which file, which function). i am just curious.

Can you explain what you mean exactly by "code generated by the compiler"?
Are  you interested in the assembly? https://golang.org/doc/asm ?
The compiler sources are in
and there is no single file/function which compiles package reflect.

This really sound like a XY problem.

V.

xie cui

unread,
Aug 14, 2020, 7:00:19 AM8/14/20
to golang-nuts
the return instance of reflect.TypeOf(some object) should be generate by compiler, the type info which could be in elf files. i want to known where compiler generate it. 

xie cui

unread,
Aug 14, 2020, 7:05:13 AM8/14/20
to golang-nuts
i am reading gc's code. i just need to know. reflect.TypeOf return a interface, the interface ref to a instance or call data. that instance should be generate by compiler. but i can not find where is the compiler code that generate data about type.

Volker Dobler

unread,
Aug 14, 2020, 8:52:54 AM8/14/20
to golang-nuts
On Friday, 14 August 2020 13:00:19 UTC+2, xie cui wrote:
the return instance of reflect.TypeOf(some object) should be generate by compiler, the type info which could be in elf files. i want to known where compiler generate it.

Maybe this is the reason for your confusion.
Your assumption that the compiler creates the value returned
by reflect.TypeOf is _plain_ _wrong_. This is _not_ done
during compile time as it  cannot be during compile time.
This happens during run time and thus _not_ by the compiler.

You cannot know where the compiler generates it  because
the compiler doesn't generate it.

The instance which is returned by reflect.TypeOf is generated
by package reflect in the two lines

V.

Mikael Gustavsson

unread,
Aug 14, 2020, 10:22:53 AM8/14/20
to golang-nuts
But those two lines don't do much, the first line reads the type pointer out of the interface, and the function toType is just a nil check: https://golang.org/src/reflect/type.go?s=38892:38923#L2963

I'm guessing xie cui is looking for where the instances of for example structType: https://golang.org/src/reflect/type.go?s=38892:38923#L435 are created and how the info for the struct fields are filled in.

xie cui

unread,
Aug 14, 2020, 12:31:03 PM8/14/20
to golang-nuts
i find the reference code in cmd/compile/internal/gc/reflect.go
Reply all
Reply to author
Forward
0 new messages