Go语言语法混淆

huangapple go评论156阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2016年4月27日 23:03:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/36893674.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定