Go Language Basics: Functions

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

A function is a core part of a program, and all programs are made up of one function.

Functions declare several formats

typetypetypetype2) {//这里是处理逻辑代码//返回多个值return value1, value2}
typetype2)(typetype2) {//这里是处理逻辑代码return value1, value2}
typetype2)type1 {//这里是处理逻辑代码return value1}
typetypetypetype2) {//这里是处理逻辑代码output1 = value1output2 = value2return}

As can be seen from the above:

    • Go uses the keyword func to declare a function funcName
    • Functions can have multiple return values, which is quite different from C, Java, etc.
    • Return value variables you can declare, or not declare, directly on two types
    • When there is only one return value, the parentheses can be omitted from the declaration
    • Initialize directly in the function, return value without any parameters, return directly

Format of the variable parameter

func myfunc(arg...int) {}

Arg...int tells go that the function accepts an indefinite number of arguments. In the function body, the variable arg is the slice of an int.

package mainimport"fmt"func myfunc(arg ...int32) {    fmt.Print(arg)    forrange arg {        fmt.Printf(" %d", n)    }}func main() {    myfunc(1, 2, 3, 4)}

Output:

[12341234

Delay defer

Defer does not execute immediately at the time of Declaration, but before the function return, executes each defer statement sequentially, followed by the FIFO principle, and then return. Similar to object-oriented destructors.
Specific mechanism reference:Go language defer, do you really understand?

Main function and init function

There are two reserved functions in Go: the init function (which can be applied to all package) and the main function (can only be applied to package main).

    • Two function definitions cannot have any parameters and return values
    • It is recommended that you write only one init function per file in each package. Although you can have more than one init function
    • The init function cannot be called by another function, but is called automatically before the main function executes.
    • The init () call order for the same go file is from top to bottom
    • Call the Init function in the order of the source file names submitted to the compiler for different files in the same package
    • For different packages, if they do not depend on each other, the Init () in the package is called in the order of the "Import first call" in the main package, and Init () in the first dependent bundle is called first if there is a dependency Finally, call the main function

Main function introduces package initialization flowchart

The initialization and execution of the program starts with the main package. If the main package also imports other packages, they are imported sequentially at compile time. Sometimes a package can be imported at the same time by multiple packages, so it will only be imported once (for example, many packages may use the FMT package, but it will only be imported once, because there is no need to import multiple times). When a package is imported, if the package also imports other packages, the other packages are imported, and then the package-level constants and variables in those packages are initialized, followed by the Init function (if any), and so on. When all the imported packages are loaded, the package-level constants and variables in the main package are initialized, and the INIT function in the main package (if present) is executed, and the main function is finally executed. The specific execution process is as follows:

Import

Point operation

import(    ."fmt")

The point operation means that after the package is imported, you can omit the prefix's package name when you call the package's function, which is the FMT you called earlier. Println ("Hello World") can be omitted as written Println ("Hello World")

Alias operation

import (    f "fmt")

Alias operation when the call packet function prefix becomes our prefix, that is f.println ("Hello World")

_ Operation

import (    "database/sql"    _ "github.com/ziutek/mymysql/godrv")

The operation is actually to introduce the package, instead of directly using the function inside the package, instead of invoking the INIT function inside the package. The operation is actually to introduce the package, instead of directly using the function inside the package, instead of invoking the INIT function inside the package.

http://blog.csdn.net/htyu_0203_39/article/details/50948193

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.