Go Language function Learning Tutorial _golang

Source: Internet
Author: User

This article describes the basic usage of the Go language function. Share to everyone for your reference, specific as follows:

Here's where the go function is different from some other languages.

1 function format is different

Copy Code code as follows:
Func getmsg (i int) (R string) {
Fmt. Println (i)
r = "HI"
Return r
}

Func shows that this is a function.

Getmsg is the name of the function

(i int) function receives an int parameter

(r string) function returns a string type return value

2 function can return multiple return values

This is not the same as c,php, it's the same as Lua.

Copy Code code as follows:
Func getmsg (i int) (R string, err string) {
Fmt. Println (i)
r = "HI"
Err = "No err"
Return R,err
}

3 Use of Defer

Defer means "call when function exits", especially when reading and writing files, you need to call the close operation after open, and use the close operation defer

Copy Code code as follows:
Func ReadFile (FilePath string) () {
File. Open (FilePath)
Defer file. Close ()
If True {
File. Read ()
} else {
return False
}
}

This is written in the file. Don't call close immediately after open, call file when return false. Close (). This effectively avoids the memory leak problem in the C language.

4 more difficult to understand: Panic,recover and defer

The role of defer is very clear in front of me.

Panic and recover, we look at them as throw and catch in other languages.

The following example:

Copy Code code as follows:
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:

Copy Code code as follows:
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 out the message and jumps out of the function. Recover received the information and continued processing.

This example understands the basics of mastering recover and panic.

I hope this article will help you with your go language program.

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.