Golang's grammatical sugar

Source: Internet
Author: User

Golang's grammatical sugar

...

There are three points in the Go language function ... represented as mutable parameters, which is the icing syntax of go, which means that you can accept any number of parameters of the same type.

func print(values...string) { //可以接受任意个string参数    for _, v:= range values{        fmt.Println("---> ",v)    }}func main(){   values := []string{"abc",  "def",  "hig",  "klm",}   print(values...) }

:=

:=Is the assignment of go and declarative syntax sugar, its function is declaration, assignment and type inference.

//第一种方法var number1  number2  number3 intnumber1, number2, number3 = 1, 2, 3 //第二种方法var number1, number2, number3 = 1, 2, 3 //第三种方法number1, number2, number3 := 1, 2, 3
  • :=Can be used only if there are undefined variables on the left (variables that are defined can also)
  • :=Only semantic checks, loops can also be used, the first time after the effect and = the same
  • :=and = both require that the left and right values correspond to the variable one by one and do not truncate
  • Special case, Map, Chan, type presumption can return a value, or it can be two values
  • Always returns a value of 0 and False if the key does not exist in map, otherwise the latter returns true
  • When Chan closes, it always returns a value of 0 and False, otherwise the latter returns true
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.