Go | Function Notes

summary of details

  1. The function's formal parameter list can be multiple, and the return value list can also be multiple
  2. The data type of the formal parameter list and the return value list, which can be a value type or a reference type
  3. The name of the function follows the identifier naming convention. The first letter cannot be a number. The uppercase letter means that the function can be used by this package file and other files, similar to public; the lowercase initial letter can only be used by this package file, similar to private.
  4. Variables in functions are local and cannot be accessed from outside. scope
  5. Basic data types and arrays are passed by value, that is, value copies are performed. Modifications within the function will not affect the original value,
  6. If you want the variable inside the function to be able to modify the variable outside the function, you can pass in the address of the variable (&), and the variable is manipulated in the form of a pointer in the function, which is similar to reference in effect
  7. Go functions do not support function overloading.
  8. In Go, a function is also a data type that can be assigned to a variable, then the variable is a function type variable through which the function can be called.
  9. Since a function is a data type, in Go, a function can also be used as a formal parameter and called. (Callback)
  10. To simplify data type definition, Go supports custom data types
  • Syntax: type custom data type name data type (equivalent to an alias)

  • Case: type myInt int At this time, myInt is equivalent to int to use

  • type mySum func(int, int) int At this time, mySum is equivalent to func(int, int) int

  1. Support for naming function return values ​​(the order of return values ​​is not limited)
  2. Use the _ underscore identifier to ignore the return value. (Placeholder)
  3. Go supports variadic arguments

passing by value and passing by pointer

func test(n1 int) {
	n1 = n1 + 10
	fmt.Println("test n1=", n1)

// Pointer type reception processing
func test02(n2 *int) {
	*n2 = *n2 + 10
	fmt.Println("test02 n2=", *n2)

func main() {
	n1 := 20
	n2 := 2
	test(n1) // value type
	test02(&n2) // pointer type
	fmt.Println("main n1=", n1)
	fmt.Println("main n2=", n2)

what is overloading

Overloading: The same function name, but different formal parameters or different data types.

Golang language does not support traditional function overloading, fn redeclared in this block

The Golang language supports variable parameters, in the form of an empty interface

variable of function type

Type: func(int, int) int

func getSum(n1 int, n2 int) int {
	return n1 + n2
func getSums(n1 int, n2 int, n3 int) int {
	return n1 + n2 + n3

// main function
sumFn := getSum
res := sumFn(10, 20)
fmt.Printf("%T %v\n", res, res) // int 30
fmt.Printf("%T \n", sumFn) // func(int, int) int

sumsFn := getSums
result := sumsFn(10, 20, 30)
fmt.Printf("result : %T %v\n", result, result) // result : int 60
fmt.Printf("sumsFn type:%T \n", sumFn) // sumsFn type: func(int, int) int

The function is passed in as a formal parameter

func getSum(n1 int, n2 int) int {
	return n1 + n2
func testFn(fnVar func(int, int) int, num1 int, num2 int) int {
	return fnVar(num1, num2) // Call the passed function and return the value

// function type parameter
sumFn := getSum
total := testFn(sumFn, 1, 2)
fmt.Println("total=", total) // 3

custom data type

  1. Custom function data type, which is equivalent to an alias
type mySum func(int, int) int

func testFn(fnVar mySum, num1 int, num2 int) int {
	return fnVar(num1, num2)

// func testFn(fnVar func(int, int) int, num1 int, num2 int) int {
// 	return fnVar(num1, num2)
// }
  1. custom data type
// under the main function
type myInt int
var num1 myInt = 2
// var num2 int = num1 // This is an error, myInt and int are not equivalent
var num2 int = int(num1) // explicit type conversion
fmt.Printf("num1 type:%T value:%v \n", num1, num1) // Type of num1: main.myInt Value: 2
fmt.Printf("num2 type:%T value:%v \n", num2, num2) // Type of num2: int Value: 2
  1. Defined type: package name. type name, such as: utils.myInt
// The following is the utils package
package utils

import "fmt"

func TestFn() string {
	fmt.Println("TestFn function is called")
	type myInt int
	var n myInt = 10
	fmt.Printf("n type:%T value:%v", n, n) // Type of n: utils.myInt Value: 10
	return "hahaha"

return value name

func sumSub(n1 int, n2 int) (sum int, sub int) {
    // There is no need to declare the sum and sub variables here, and there is no need to write them when return ing
	sum = n1 + n2
	sub = n1 - n2

// main function
sum, sub := sumSub(9, 8)
fmt.Println("sum=", sum, "sub=", sub) // sum= 17 sub= 1

variable parameter

basic grammar

  1. Supports zero to many arguments

func sum(args... int) {}

  1. Supports 1 or more parameters

func sum(n1 int, args... int) {}

args: It is an inherited variable name, which can be customized, such as: func sum(n1 int, **vars**... int) {}


  • args is a slice slice, each value can be accessed through args[index]
  • args must be placed at the end of the parameter list

Variable number of parameters

func sumV2(n1 int, args ...int) int {
	sum := n1
	fmt.Printf("args The type is:%T\n", args) // args type is: []int
	// Traverse the args slice
	for i := 0; i < len(args); i++ {
		sum += args[i]
	return sum

// main function
// variable parameters
total02 := sumV2(1, 2, 3, 4)
fmt.Println("total02=", total02) // total02= 10

Summary exercise

Swap the values ​​of variables a, b

package main

import "fmt"

func swap(n1 *int, n2 *int) {
	*n1 = *n1 + *n2
	*n2 = *n1 - *n2 // *n1
	*n1 = *n1 - *n2 // *n2

func main() {
	a := 12
	b := 20
	swap(&a, &b)
	fmt.Println("a = ", a, "b = ", b)

I'm dessert cc

WeChat public account: [see another possibility]

I love front-end development, and I also like to specialize in various technologies that have little to do with my job. I have a wide and strong interest in technology and products. This account is mainly dedicated to sharing personal experience summaries, hoping to give some small help to a small number of people.

I hope to work with you to create a good learning atmosphere, and make a little contribution to individuals and families, to my country's Internet of Things technology, digital transformation, and digital economic development. Counting romantic figures still look at China, at the present, at you and me.

Tags: Go

Posted by jmansa on Thu, 24 Nov 2022 12:38:42 +0300