缺少字段或方法

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

Missing Field or Method

问题

var client := http.Client

由于某种原因,这段代码出现了错误消息“缺少变量或初始化”。有人可以告诉我为什么吗?我不明白我做错了什么。

英文:
var client := http.Client

For whatever reason this code is giving the error message missing variable or initialization. Can someone enlighten me on why? I'm not understanding what I have done wrong.

答案1

得分: 0

在Go语言中,我们使用:=var =来初始化变量。在你的情况下,你可以将其重写为:

var client = http.Client{}

或者

client := http.Client{}

这两种方式都会触发变量的类型推断。你也可以使用var和类型来显式声明一个类型。在你的情况下,如果你想要强制指定类型,你可以这样写:

var client http.Client = http.Client{}
英文:

In Go we use := or var = for initializing variables. In your case you can re-write it to be:

var client = http.Client{}

or

client := http.Client{}

Either of these will trigger type inference of the variable. You can use var with a type to explicitly declare a type as well. In your case if you wanted to enforce the type you could write:

var client http.Client = http.Client{}

huangapple
  • 本文由 发表于 2022年7月28日 06:03:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/73145080.html
匿名

发表评论

匿名网友

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

确定