英文:
Go language Syntax Confusion
问题
所以我刚开始学习Go语言,因为并发性能的原因,我从Python转过来了。
无论如何,我在查看net/http包文档时偶然发现了这段代码:
client := &http.Client{
CheckRedirect: redirectPolicyFunc
}
我看到我们通过引用原始的Client结构体来创建一个client变量(我想这是你会这样说的),但是我完全不明白
CheckRedirect: redirectPolicyFunc
这个冒号是什么意思,我们在做什么?还有它前面和后面的那些东西是什么?我阅读了结构体的文档并完成了Go语言的入门教程,但是我没有看到任何相关内容,可能是我没有仔细寻找。毫无疑问,这很简单,只是我不知道从哪里开始找答案。
谢谢大家的回答!现在我明白多了!
英文:
So I'm brand new to Go, I am moving over from python b/c of concurrecny.
Anyways I was looking at the net/http package documentation and stumbled upon this:
client := &http.Client{
CheckRedirect: redirectPolicyFunc
}
So I see we are creating a client variable by referencing the original Client structure (I think that is how you would word that) but I am totally lost at the
CheckRedirect: redirectPolicyFunc
What the heck does the ":" mean and what are we doing with it? Also what are the things before and after it? I read the struct documentation and did the introduction to go tutorial but I didn't see anything, I may not of been looking hard enough. No doubt its simple I just have no idea where to start looking for answers.
Thanks for the answers everyone! This makes much more sense now!
答案1
得分: 6
这被称为复合字面量。
你只是创建了一个http.Client
类型的实例,并设置了CheckRedirect
属性,然后取了它的指针。
英文:
This is called a composite literal.
You're just creating an instance of the http.Client
type and setting the
CheckRedirect
property, then taking a pointer of it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论