Go language analysis

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. The

It's almost like the C language.

Example

Description

function format is different

Func getmsg (i int) (R string) {

Fmt. Println (i)

r = "HI"

Return r

}

Func indicates that this is a function

Getmsg is the name of the function

The (i int) function receives an int parameter

The (R string) function returns a string of type return value

function can return multiple return values

Func getmsg (i int)   (r  string, err string)  {

    fmt. Println (i)

    r =  "HI"

    err =  "No"  err "

    return r,err

 }

This is not the same as C, PHP, and Lua is the same.

Use of defer

Func ReadFile (FilePath string) () {

File. Open (FilePath)

defer file. Close ()

If True {

File. Read ()

} else {

return False

}

}

Defer means "call on function exit", especially when reading and writing to a file, you need to call close after open, and the close operation uses defer, which is written in file. Close is not immediately called after open, and file is called when return is false. Close (). This effectively avoids the problem of memory leaks in the C language.

Note: The original code needs to close the file when the program returns true and false, so that it is only needed once and will not be forgotten.

Panic, recover

Package Main

Import "FMT"

Func Main () {

F ()

Fmt. PRINTLN ("returned normally from F.")

}

Func f () {

Defer func () {

If r: = recover(); R! = Nil {

Fmt. Println ("Recovered in F", R)

}

}()

Fmt. Println ("calling G.")

G (0)

Fmt. PRINTLN ("returned normally from G.")

}

Func g (i int) {

If I > 3 {

Fmt. Println ("panicking!")

Panic (FMT. Sprintf ("%v", I))

}

defer FMT. Println ("Defer in G", I)

Fmt. Println ("Printing in G", I)

G (i + 1)

}

Returned the following:

Calling G.

Printing in G 0

Printing in G 1

Printing in G 2

Printing in G 3

panicking!

Defer in G 3

Defer in G 2

Defer in G 1

Defer in G 0

Recovered in F 4

Returned normally from F.

Panic throws the message and jumps out of the function. The recover receives the information and continues processing.

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.