search
HomeBackend DevelopmentGolangIntroduction to functions and methods in go language

Introduction to functions and methods in go language

Jan 06, 2020 pm 05:51 PM
go languagefunction

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:博客园. If there is any infringement, please contact admin@php.cn delete
Building Scalable Systems with the Go Programming LanguageBuilding Scalable Systems with the Go Programming LanguageApr 25, 2025 am 12:19 AM

Goisidealforbuildingscalablesystemsduetoitssimplicity,efficiency,andbuilt-inconcurrencysupport.1)Go'scleansyntaxandminimalisticdesignenhanceproductivityandreduceerrors.2)Itsgoroutinesandchannelsenableefficientconcurrentprogramming,distributingworkloa

Best Practices for Using init Functions Effectively in GoBest Practices for Using init Functions Effectively in GoApr 25, 2025 am 12:18 AM

InitfunctionsinGorunautomaticallybeforemain()andareusefulforsettingupenvironmentsandinitializingvariables.Usethemforsimpletasks,avoidsideeffects,andbecautiouswithtestingandloggingtomaintaincodeclarityandtestability.

The Execution Order of init Functions in Go PackagesThe Execution Order of init Functions in Go PackagesApr 25, 2025 am 12:14 AM

Goinitializespackagesintheordertheyareimported,thenexecutesinitfunctionswithinapackageintheirdefinitionorder,andfilenamesdeterminetheorderacrossmultiplefiles.Thisprocesscanbeinfluencedbydependenciesbetweenpackages,whichmayleadtocomplexinitializations

Defining and Using Custom Interfaces in GoDefining and Using Custom Interfaces in GoApr 25, 2025 am 12:09 AM

CustominterfacesinGoarecrucialforwritingflexible,maintainable,andtestablecode.Theyenabledeveloperstofocusonbehavioroverimplementation,enhancingmodularityandrobustness.Bydefiningmethodsignaturesthattypesmustimplement,interfacesallowforcodereusabilitya

Using Interfaces for Mocking and Testing in GoUsing Interfaces for Mocking and Testing in GoApr 25, 2025 am 12:07 AM

The reason for using interfaces for simulation and testing is that the interface allows the definition of contracts without specifying implementations, making the tests more isolated and easy to maintain. 1) Implicit implementation of the interface makes it simple to create mock objects, which can replace real implementations in testing. 2) Using interfaces can easily replace the real implementation of the service in unit tests, reducing test complexity and time. 3) The flexibility provided by the interface allows for changes in simulated behavior for different test cases. 4) Interfaces help design testable code from the beginning, improving the modularity and maintainability of the code.

Using init for Package Initialization in GoUsing init for Package Initialization in GoApr 24, 2025 pm 06:25 PM

In Go, the init function is used for package initialization. 1) The init function is automatically called when package initialization, and is suitable for initializing global variables, setting connections and loading configuration files. 2) There can be multiple init functions that can be executed in file order. 3) When using it, the execution order, test difficulty and performance impact should be considered. 4) It is recommended to reduce side effects, use dependency injection and delay initialization to optimize the use of init functions.

Go's Select Statement: Multiplexing Concurrent OperationsGo's Select Statement: Multiplexing Concurrent OperationsApr 24, 2025 pm 05:21 PM

Go'sselectstatementstreamlinesconcurrentprogrammingbymultiplexingoperations.1)Itallowswaitingonmultiplechanneloperations,executingthefirstreadyone.2)Thedefaultcasepreventsdeadlocksbyallowingtheprogramtoproceedifnooperationisready.3)Itcanbeusedforsend

Advanced Concurrency Techniques in Go: Context and WaitGroupsAdvanced Concurrency Techniques in Go: Context and WaitGroupsApr 24, 2025 pm 05:09 PM

ContextandWaitGroupsarecrucialinGoformanaginggoroutineseffectively.1)ContextallowssignalingcancellationanddeadlinesacrossAPIboundaries,ensuringgoroutinescanbestoppedgracefully.2)WaitGroupssynchronizegoroutines,ensuringallcompletebeforeproceeding,prev

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools