Home > Blockchain >  Missing Filed or Method
Missing Filed or Method

Time:07-28

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

CodePudding user response:

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{}
  •  Tags:  
  • go
  • Related