About Go functions

Basic syntax of the function

The basic form of the function definition is as follows.

func function name(argument)Return type{
Contents of processing...
}

It is a flow to take an argument for a function, specify the return value of the function, and write the processing content.

Named function

Now let's define a simple function.

main.go


func test(x, y int) int {
   a := x * y
   return a
}

func main() {
   fmt.Println(test(10, 5))  //50
}

The function test is defined by int type x and y, and int type is specified as the return type. The process is assigned to the variable a, the returned value is received by the main function, and the value is specified and output.

Also, multiple return values can be returned.

main.go


func test(x, y int) (int, int) {
   a := x * y
   b := x / y
   return a, b
}

func main() {
   a, b := test(10, 5)
   fmt.Println(a, b)  //50 2
}

You can also define a function without a return value by omitting the return type, as shown below.

main.go


func test() {
	fmt.Println("Hello!")
	return
}

func main(){
   test()  //Hello!
}

Error handling

There are the following idioms for error handling.

main.go


import (
   "os"
   "log"
)

func main() {
   _, err := os.Open("test.txt")
   if err != nil {
      log.Fatal(err)
  }
}

This program is a process that outputs an error message if there is an error when opening a file. Therefore, I am importing the os package and the log package.

Anonymous function

Anonymous functions, as the name implies, define functions as "anonymous", unlike named functions that are explicitly defined.

main.go


func main() {
   a := func(x, y int) int { return x + y }
   fmt.Println(a(2, 5))  //7
}

You can also draw even shorter by taking an argument to the anonymous function as it is.

main.go


func main() {
   fmt.Println(func(x, y int) int { return x + y }(2, 5))  //7
}

This prints the same for anonymous function definitions using function literals.

Function returns a function

You can return a function in a function by using an anonymous function.

main.go


func test() func() {
   return func() {
      fmt.Println("output")
   }
}

func main() {
   a := test()
   a()  //output
}

This program implicitly assigns a function that has no arguments and no return value as a return value to the variable a and outputs it.

A function that takes a function as an argument

Similarly, it is possible to take a function as an argument.

main.go


func test(a func()) {
   a()
}

func main() {
   test(func() {
      fmt.Println("output")  //output
   })
}

In this program, ** a function that has neither an argument nor a return value ** is taken as an argument of the function test, and what is called is output by passing it as an anonymous function in the function main.

Generator that applies closure

In Go, anonymous functions are ** closures **, like function closures. Normally, variables defined in a function (local variables) are destroyed after the execution of the function is completed, but by connecting with a closure, it is not destroyed as long as the closure is referenced. There is no. By using this logic, it is possible to realize ** generator-like functions ** that Go does not have.

main.go


func test() func() int {
   a := 0  //Local variables referenced from within the closure
   return func() int {
      a++
      return a  //Local variables(Variables that belong to closures)Referencing
   }
}

func main() {
   b := test()

   fmt.Println(b())  //1
   fmt.Println(b())  //2
   fmt.Println(b())  //3
   fmt.Println(b())  //4
}

Finally

This time, I summarized the functions! In Go, which does not have object-oriented functions, the definition of functions will be central.

From now on, I will summarize the control syntax! !!

Recommended Posts

About Go functions
About Go Interface
Learn about Go slices
Note about pointers (Go)
[Python] Memo about functions
About Go control syntax
About Go GET request
About Go Modules (vgo)
[Golang] About Go language channel
About the basic type of Go
Go Language-Basic ❶
Go language-fmt.Scan
About LangID
About CAGR
About virtiofs
About python-apt
Go language-address
About sklearn.preprocessing.Imputer
About gunicorn
About requirements.txt
About permissions
About Opencv ②
Go Language-Functions
Go language-pointer
About Opencv ③
Go Language-Basic ❷
About import
Python functions
Built-in functions
GO Chokisomemo 1
Go language-iteration
About numpy
About pip
About Linux
About numpy.newaxis
About endian
About Linux
About import
About Opencv ①
About Linux
About Linux
About Linux ①
About cv2.imread
[Golang] About Go language Producer and Consumer
About _ and __
About wxPython
About Nim higher-order functions for Nim beginners written by Nim beginners
A story about modifying Python and adding functions