Skip to content

Can not find the Struct type #18

@zhengzepeng

Description

@zhengzepeng

Show my code.

1、utils package
It define the student struct,like that:

package utils

import (
	"fmt"
)

type Student struct {
	Name string
}

func (s *Student) PrintName() {
	fmt.Println(s.Name)
}

2、Use the Student
The main function use reflect2 to find the student type,like that:

package main

import (
	_ "example/testreflect/utils"
	"fmt"
	"github.com/modern-go/reflect2"
)

func main() {
	tp := reflect2.TypeByPackageName("example/testreflect/utils", "Student")
	fmt.Println(tp)
}

the output is :

<nil>

it cant find the student.

3、change the way to use the Student

package main

import (
	"example/testreflect/utils"
	"fmt"
	"github.com/modern-go/reflect2"
)

func main() {
	fmt.Println(utils.Student{})
	tp := reflect2.TypeByPackageName("example/testreflect/utils", "Student")
	fmt.Println(tp)
}

and the output is :

{}
utils.Student

It Works now.

So it confuse me....can anyone help?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions