Home >Backend Development >Golang >How to write functions in golang

How to write functions in golang

silencement
silencementOriginal
2019-12-25 10:11:162847browse

How to write functions in golang

The way to define functions in Go is as follows:

func (p myType ) funcName ( a, b int , c string ) ( r , s int ) {    
return
}

Through function definition, we can see the commonalities and characteristics of functions in Go and other languages

Commonness

Keyword——func

Method name——funcName

Input parameters———— a,b int,b string

Return value—— r,s int

Function body—— {}

Features

The features of functions in Go are very cool and bring us something different Programming experience.

Define a function for a specific type, that is, define a method for the type object

In Go, you define a method for the type by marking the type of the function. The above p myType means declaring myType There is a method, p myType is not necessary

. If not, it is purely a function, accessed via the package name. packageName.funcationName

ype double float64

//判断a是否等于b
func (a double) IsEqual(b double) bool {
    var r = a - b
    if r == 0.0 {
        return true
    } else if r < 0.0 {
        return r > -0.0001
    }
    return r < 0.0001
}

is recommended to study "golang tutorial".

The above is the detailed content of How to write functions in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn