Home  >  Article  >  Backend Development  >  Introduction to functions and methods in go language

Introduction to functions and methods in go language

尚
forward
2020-01-06 17:51:362656browse

Introduction to functions and methods in go language

If you encounter a function declaration without a function body, it means that the function is not implemented in Go.

package math
func Sin(x float64) float //implemented in assembly language

If a variable name is set for each return value of a function, it will be initialized with the corresponding zero value, and the operand will be omitted in the return statement of the function. This usage is called bare return.

The error handling in Go is customary to perform a series of initialization checks first, and process the code that handles the failure logic first, and then the actual logic of the function. This makes the code more concise and avoids too many errors. Hierarchy.

When defining a function, you can use the function type as a parameter or as a return type. Is it a bit like a delegate to achieve closure? There are also anonymous functions, which are similar to lambda expressions. The strings.Map function can be used for experimentation.

func squares() func() int {
    var x int
    return func() int {
        x++
        return x * x
    }
}
func main() {
    f := squares()
    fmt.Println(f()) // "1"
    fmt.Println(f()) // "4"
    fmt.Println(f()) // "9"
    fmt.Println(f()) // "16"
}

There are variable references in anonymous functions and squares. This is why function values ​​are reference types and function values ​​are not comparable. Go uses closure technology to implement function values, and Go programmers also call function values ​​closures.

Pay attention to the example program in the anonymous function section of the golang Bible.

The variable parameter function of the Go language is very easy to use. You can pass multiple parameters of the same type, or you can directly pass in a slice of this type (note that you must use the... mark when passing in the slice. I I think it is to distinguish the same slice parameters, after all, the two are still somewhat different). If you want to use different types of variable parameters, then use the universal interfac{}, and just parse the variable parameters in the function body like a slice. .

Until the function containing the defer statement is executed, the function after the defer will not be executed, regardless of whether the function containing the defer statement ends normally through return or ends abnormally due to panic. You can execute multiple defer statements in a function, and their execution order is opposite to the declaration order

var mu sync.Mutex
var m = make(map[string]int)
func lookup(key string) int {
    mu.Lock()
    defer mu.Unlock()
    return m[key]
}

When debugging complex programs, the defer mechanism is also often used to record when to enter and exit a function.

func bigSlowOperation() {
    defer trace("bigSlowOperation")() // don't forget the
    extra parentheses
    // ...lots of work…
    time.Sleep(10 * time.Second) // simulate slow
    operation by sleeping
}
func trace(msg string) func() {
    start := time.Now()
    log.Printf("enter %s", msg)
    return func() { 
        log.Printf("exit %s (%s)", msg,time.Since(start)) 
    }
}

We only need to name the return value of double first, and then add a defer statement. We can output the parameters and return value every time double is called.

func double(x int) (result int) {
    defer func() { fmt.Printf("double(%d) = %d\n", x,result) }()
    return x + x
}
_ = double(4)
// Output:
// "double(4) = 8"

To facilitate problem diagnosis, the runtime package allows programmers to output stack information. In the following example, we output stack information by delaying the call to printStack in the main function.

gopl.io/ch5/defer2
func main() {
    defer printStack()
    f(3)
}
func printStack() {
    var buf [4096]byte
    n := runtime.Stack(buf[:], false)
    os.Stdout.Write(buf[:n])
}

It is a bit strange that field names and method names with the same name cannot be defined for a structure.

Function pointer: There are actually function pointers in go. Let’s use go language to implement the table-driven mode.

package main

import (
    "fmt"
)

func add(a int, b int) int {
    return a + b 
}

func sub(a int, b int) int {
    return a - b 
}

func main() {
    fm := make(map[int]func(int, int) int)
    fm[1001] = add 
    fm[1002] = sub 
    protocol := 2001
    i := 1
    j := 2
    if func_handle, ok := fm[protocol]; ok {
        println(func_handle(i, j)) 
    } else {
        fmt.Printf("protocol: %d not register!", protocol)
    }   
}

Return local variable pointer:

Unlike C language, GO functions can return local change pointers, and the compiler will use escape analysis to decide whether to Allocate memory on the heap.

You can disable function inlining through the -gcflags "-l -m" parameter when compiling. Function inlining will have some impact on memory allocation, but the details are unclear.

There is no so-called reference passing for function parameters, they are all passed by value. The only difference is whether a copy object or a pointer is passed. In C language, it is generally recommended to pass pointer parameters to avoid copying objects and improve efficiency.

But in go, the copied pointer will extend the life cycle of the target object, and may also cause it to be allocated on the heap. The performance consumption will be added to the cost of heap memory allocation and garbage collection, and in Copying small objects on the stack is actually very fast, so if the object is not particularly large or the original object really needs to be modified, there is generally no need to pass pointer parameters. In concurrent programming, the use of immutable objects (read-only or copied) is also advocated, which can eliminate the trouble of data synchronization.

The following will allocate memory on the heap. When compiling, pass -gcflags "-m" to view the assembly code:

func test(p *int) {
    go func() {
        println(p)
    }() 
}

func main() {
    x := 100 
    p := &x
    test(p)
}

Use outgoing parameters, it is recommended to use the return value, you can also use two Level pointer:

func test(p **int) {
    x := 100
    *p = &x
}

func main() {
    var p *int
    test(&p)
    println(*p)
}

For more go language knowledge, please pay attention to the go language tutorial column on the PHP Chinese website.

The above is the detailed content of Introduction to functions and methods in go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete