gpt4 book ai didi

go - 我可以在包内创建一个结构以在主程序外部使用吗

转载 作者:IT王子 更新时间:2023-10-29 01:42:11 25 4
gpt4 key购买 nike

我正在将我的 npm 结节移植到 go package,在一个地方我需要这样做。

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"
}
}

参见 Exported identifiers

关于go - 我可以在包内创建一个结构以在主程序外部使用吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36967419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com