英文:
How to get all defined types?
问题
在这个包中,你可以通过导入demo
包并使用反射来获取所有从demo
包导出的类型。以下是一个示例代码:
package main
import (
"demo"
"fmt"
"reflect"
)
func main() {
// 获取demo包的类型
demoTypes := reflect.TypeOf(demo.People{}).PkgPath()
fmt.Println(demoTypes)
// 获取demo包中所有导出的类型
pkg, err := reflect.Import("demo", "./")
if err != nil {
fmt.Println(err)
return
}
for _, typ := range pkg.Types {
fmt.Println(typ.Name())
}
}
这段代码将输出demo
包中所有导出的类型的名称。请注意,你需要将代码中的demo
包路径和导入路径调整为你实际的情况。
英文:
package demo
type People struct {
Name string
Age uint
}
type UserInfo struct {
Address string
Hobby []string
NickNage string
}
another package:
import "demo"
in this package, how can I get all the types exported from the demo package?
答案1
得分: 5
使用go/importer
:
pkg, err := importer.Default().Import("time")
if err != nil {
fmt.Println("错误:", err)
return
}
for _, declName := range pkg.Scope().Names() {
fmt.Println(declName)
}
(注意,在Go Playground上会返回一个错误)。
英文:
Using go/importer
:
pkg, err := importer.Default().Import("time")
if err != nil {
fmt.Println("error:", err)
return
}
for _, declName := range pkg.Scope().Names() {
fmt.Println(declName)
}
(note, this returns an error on the Go Playground).
答案2
得分: 3
Go语言在包级别上没有保留结构体、接口或变量的主列表,所以你所问的是不可能的。
英文:
Go retains no master list of structs, interfaces, or variables at the package level, so what you ask is unfortunately impossible.
答案3
得分: 1
真糟糕,我希望Jsor的答案是错误的,但是我找不到任何方法来实现它。
不过还不算完全失败:如果你有"demo"的源代码,你可以使用"parser"包来提取你所需的信息。虽然有点取巧。
英文:
Drat, I was hoping that Jsor's answer was wrong, but I can't find any way to do it.
All is not lost though: If you have the source to 'demo', you could use the parser
package to fish out the information you need. A bit of a hack though.
答案4
得分: 1
你是来自某种脚本语言吗?看起来是这样的。
Go语言有很好的理由来防止在代码中出现“魔法”。
在开始时看起来很容易(可以访问所有的结构体,在某个地方自动注册它们,“节省”编码),但当你的项目变得更大时,这将变成一个调试和维护的噩梦。
然后你将不得不记录和查找所有你糟糕的约定和含义。
我知道我在说什么,因为我用Ruby和Node.js走过这条路线多次。
相反,如果你让一切都显式化,你会得到一些功能,比如将People
结构体重命名,让编译器告诉你在整个代码库中它被使用的地方(而且Go编译器比你快)。
这样的可能性对于调试、测试和重构是无价的。
同时,它使得你的代码易于理解,对于你的同行和你自己在几个月后编写它时也是如此。
英文:
Do you come from some scripting language? It looks so.
Go has good reasons to propagate to let not slip 'magic' into your code.
What looks easy at the beginning (have access to all structs, register them automatically somewhere, "saving" coding) will end up in debugging and maintenance nightmare when your project gets larger.
Then you will have to document and lookup all your lousy conventions and implications.
I know what I am talking about, because I went this route several times with ruby and nodejs.
Instead, if you make everything explicit you get some feature, like renaming the People
struct to let
the compiler tell you where it is used in your whole code base (and the go compiler is faster than you).
Such possibilities are invaluable for debugging, testing and refactoring.
Also it makes your code easy to reason about for your fellow coders and yourself several months after you have written it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论