You may not know some of the go packages knowledge

Source: Internet
Author: User

About Go package about Go package

Go Packages is primarily used to organize related functions, variables, and constants so that you can easily migrate Packages and use them in your own programs.

Note that in addition to the main package, Go packages is not an autonomous program and cannot be compiled into an executable file. They must be called directly or indirectly by the main package to be used.
If you are directly executing a package without a package:

$ go Run apackage.go
Go run:cannot Run Non-main package

About Go function about Go function

anonymous function (anonymous functions)
Anonymous functions can be defined internally without the use of function names, and anonymous functions are often used to implement functions that do not require much code. In go, a function can return an anonymous function, or use an anonymous function as one of its parameters. In addition, anonymous functions can be accessed through a go variable. Note that anonymous functions are also known as closures, especially in functional programming terms.
Anonymous functions have small implementations and local focus, which is considered a good practice. If the anonymous function does not have local focus, you might want to consider making it a regular function.
Be careful not to use a large number of anonymous functions in your program when there is no special need.

Go function can return multiple values

Func afunction () (int, int, float64, float64) {
}

Here's an anonymous function that shows go with a functions.go

package mainimport (    "fmt"    "os"    "strconv")func doubleSquare(x int) (int, int) {    return x * x, x * x }func main() {    arguments := os.Args    if len(arguments) != 2 {        fmt.Println("The program needs 1 argument!")        return    }    y, err := strconv.Atoi(arguments[1])    if err != nil {        fmt.Println(err)        return    }    square := func (s int) int {        return s * s     }    fmt.Println("The  square of", y, "is", square(y))    double := func (s int) int {        return s + s    }    fmt.Println("The double of", y, "is", double(y))    fmt.Println(doubleSquare(y))    d, s := doubleSquare(y)    fmt.Println(d, s)}

The square and double above hold an anonymous function. The downside is that in later programs you can change the value of square,double or the other variables to the anonymous function, which means that the meaning of these variables can be changed and other content is calculated.

Modifying the value of an anonymous function variable is not recommended, because this can be the main cause of a very difficult bug to troubleshoot.

As shown above, we can print the return value of Doublesquare () directly or assign them different variables to print.

Execution Functions.go:

$ go run function.go 1 21 The program needs 1 argument!rMacBook:code mtsouk$ go run functions.go 10The square of 10 is 100The double of 10 is 2020 10020 100

The return value of the function can be named by the return value of the named function.

Let's take returnnames.go as an example, and we'll split the Returnnames.go code into 3 parts to explain
Part 1

package mainimport (    "fmt"    "os"    "strconv")func nameMinMax(x, y int) (min, max int) {    if x > y {        min = y        max = x    } else {        min = x        max = y    }    return}

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.