英文:
Setting constants in golang outside of function as how it's done in Python
问题
在Python中,我们可以在函数外部声明任何变量。我们可以简单地调用
a = 'a string'
或 b = ['asd', 'sdf', 'dfg']
我正在学习golang,并希望能够做同样的事情,简单地创建一个变量并给它赋值。
我必须在一个函数内部进行吗?它必须在func main
中吗?
到目前为止,我有以下代码:
func main() {
var A_LIST []string
A_LIST = append(A_LIST, "str1", "str2")
var B_LIST []string
B_LIST = append(B_LIST, "str3", "str4")
}
如果我想将其构建为一个.so文件,以便我的Python脚本可以导入它,这样做对吗?
编辑(更新):根据建议编写了一些代码来解释我的意图
我想要几个列表来对我的标签进行分类。在Python中,它看起来像这样:
tag_category1 = ['tag1', 'tag2', 'tag3']
tag_category2 = ['tag4', 'tag5', 'tag6']
该函数将接收一个标签列表并对其进行分类。
tags = ['tag1', 'tag3', 'tag6', 'tag7']
new_tags = {}
for tag in tags:
if tag in tag_category1:
new_tag['category1'].append(tag)
elif tag in tag_category2:
new_tag['category2'].append(tag)
else:
new_tag['others'].append(tag)
return new_tags
因此,结果应该是:
new_tags = {
'category1': ['tag1', 'tag3'],
'category2': ['tag6'],
'others':['tag7']
}
我想用Go语言编写这个函数,并且由于这个函数最初是在另一个Python脚本中使用的,我希望能够导入它。对于那个Python脚本,我仍然无法找到一些我在Go中使用的实用库,所以我必须继续使用Python。
英文:
In Python, we can declare any variable outside of a function. We can simply call
a = 'a string'
or b = ['asd', 'sdf', 'dfg']
I'm learning golang and would like to do the same to simply create a variable and assign values to it.
Do I have to do it inside a func? does it have to be in func main?
so far I have
func main() {
var A_LIST []string
A_LIST = append(A_LIST, "str1", "str2")
var B_LIST []string
B_LIST = append(B_LIST, "str3", "str4")
}
which can build and I can print out the two variables if I write a fmt.Println
but if I want to build this into a .so file so my python scripts can import it, is this the right thing to do?
EDIT (UPDATE): following the suggestion and writing out some code to explain what I'm intending to do
I want to have several lists to categorize my tags. In Python, it would look like:
tag_category1 = ['tag1', 'tag2', 'tag3']
tag_category2 = ['tag4', 'tag5', 'tag6']
The function would take in a list of tags and categorize them.
tags = ['tag1', 'tag3', 'tag6', 'tag7']
new_tags = {}
for tag in tags:
if tag in tag_category1:
new_tag['category1'].append(tag)
elif tag in tag_category2:
new_tag['category2'].append(tag)
else:
new_tag['others'].append(tag)
return new_tags
so the result should be
new_tags = {
'category1': ['tag1', 'tag3'],
'category2': ['tag6'],
'others':['tag7']
}
I wanna write this function in go, and, since this function was originally used in another python script, I want to be able to import it. For that python script, I still have issue finding some util libraries i use in go, so I have to remain using python.
答案1
得分: 1
不,你不需要在函数内部声明变量。不过,你不能使用append,因为append是一个非声明语句,不能在函数外部使用。相反,你可以使用字面量来声明变量,例如使用切片字面量:
var A_LIST = []string{"str1", "str2"}
你可以在crypto.go包的第42行开始看到一个示例:
var digestSizes = []uint8{
MD4: 16,
... //此处省略
RIPEMD160: 20,
}
你可以在playground中看到它的工作原理。
如果你需要做更多的事情而不仅仅是硬编码的值,这种方法似乎不太可行。对于任何更动态的需求,你将需要一个函数。这可以是一个init()函数、main()函数或任何其他函数,只要你在声明变量的时候意识到变量的作用域,并且了解变量/函数初始化/调用的顺序。
关于.so文件和声明这些变量的适当位置,我不清楚。尝试不同的方法,看看哪些方法有效,然后尝试弄清楚原因。
英文:
No, you don't have to declare variables inside a function. Though, you can't use append because append is a non-declaration statement, which you can't use outside a function. Instead, you can declare your variables using literals, in this case a slice literal, ie:
var A_LIST = []string{"str1", "str2"}
You can see an example of this in the crypto.go package, beginning on line 42:
var digestSizes = []uint8{
MD4: 16,
... //omitted for this post
RIPEMD160: 20,
}
See that it works in the playground
If you need to do more then just hard-code the values, this approach doesn't seem very viable. For anything more dynamic, you will need a function. This could be an init() function, main(), or any other function, as long as you are conscious of the scope of the variable when/where you declare it, and the order in which the variables/functions will be initialised/called.
Regarding the .so file and the appropriate place to declare these variable, I don't know. Try different ways and see what does/doesn't work, then try to figure out why.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论