[Go language] Learn go--if statements and map structures from Docker source

Source: Internet
Author: User

If statement

Continue to see the main function of the Docker.go file

if reexec. Init () {        return    }

If the go language does not need to be bracketed like any other language, and you can add an assignment statement before judging

Grammar

" if " " ; " " Else " (ifstmt | Block)].

Example

if x: = f (); x < y {    returnElseif x > z {    return   Else  {    return  y}

Map structure

Continue to follow the reexec. Init () method inside, file located in Docker/reexec/reexec.go

varRegisteredinitializers = Make (map[string]func ()) ...//Init is called as the first part of the exec process and returns true if an//initialization function was called.Func Init ()BOOL{initializer, exists:= Registeredinitializers[os. args[0]]    ifexists {initializer ()return true    }    return false}

You can see that registeredinitializers is a map type with a key type of string and a value type of func ().

The initialization of map is done by make (), which is the built-in method of go.

In addition to making, you can initialize it by using a method similar to the following

mapcreated: = map[stringint  {                        1int {return },                        2int {return),                        3  int {return,},                    }

Continue to see the use of maps through code

initializer, exists: = Registeredinitializers[os. args[0]]

Map[keyvalue] Returns two values, first to see the second one, to indicate whether there is a value, the first is to get the values in the case of existence, and here is to get this func ().

The next code is to determine if there is a func () to execute if it exists.

If we do not want to take value, we just want to know if it exists, we can omit the return by adding an underscore _, for example

_, exists: = XXX

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.