Go 我可以在包内创建一个结构来在主程序中使用吗
我正在移植我的npm结节去包,在一个地方我需要这样做Go 我可以在包内创建一个结构来在主程序中使用吗,go,Go,我正在移植我的npm结节去包,在一个地方我需要这样做 type Credentials struct { key string responseType string subscription string locale string } type WwogcParams struct { name string value string } func main() { param1 := WwogcParams{"q","Delhi
type Credentials struct {
key string
responseType string
subscription string
locale string
}
type WwogcParams struct {
name string
value string
}
func main() {
param1 := WwogcParams{"q","Delhi"}
wwogc := []WwogcParams{param1}
credentials := Credentials{key: "keykeykle",responseType: "json",subscription: "premium",locale: "EN"}
....
}
这里的main函数基本上是用户必须编写的,但是如果用户可以使用我在其main()函数包中定义的结构,我会怎么想
为什么不能在包外使用结构?字段使用大写名称。只有大写名称在包外部可见
package something
type Credentials struct {
Key string
ResponseType string
Subscription string
Locale string
}
type WwogcParams struct {
Name string
Value string
}
大体上:
package main
import (
"something"
)
func main() {
param1 := something.WwogcParams {"q","Delhi"}
wwogc := []something.WwogcParams {param1}
credentials := something.Credentials {
Key: "keykeykle",
ResponseType: "json",
Subscription: "premium",
Locale: "EN"
}
}
请参见意思是,如果我将“name”更改为“name”,我可以使用包主导入“wwo”//my任意包名wwo.WwogcParams{“q”,“德里”}。因此,如果您有Foo struct{abc int}类型,那么Foo是可见的,而abc不是。键入Foo struct{Abc int},现在两者都是。这就是为什么您会在godocs(即:)中看到“包含已过滤或未报告的字段”。单击标题并进入您看到mu、m和hosts字段的位置,所有这些字段都是未报告的Hanks,我目前正在尝试此操作,并将在其工作后立即接受。另请注意:您需要在use in main前面加上包的名称(例如mypack.Credentials)。包应该位于与包同名的子文件夹中,并且您应该在主文件中导入包(通过从GOPATH指定路径)。完成和完成。。。真是太棒了。。。发布了我的第一个软件包worldweatheronline go客户端