banner
biuaxia

biuaxia

"万物皆有裂痕,那是光进来的地方。"
github
bilibili
tg_channel

【Reprint】Parameters of golang functions

title: [Reprint] Parameters of Golang Functions
date: 2022-06-17 16:58:00
toc: false
index_img: http://api.btstu.cn/sjbz/?lx=m_dongman&cid=4
category:

  • Go
    tags:
  • Golang

Original: Parameters of Golang Functions - Golang Tech Stack, Golang Articles, Tutorials, and Video Sharing!. This site is only for saving records and adjusting formatting, and does not engage in profit-oriented commercial activities.

Note: This article mentions passing pointers as types!

Go language functions can have 0 or more parameters, and the data type needs to be specified.

The parameter list when declaring a function is called a formal parameter, and the parameters passed when calling it are called actual parameters.

In Go language, parameters are passed by value, which means that a copy is passed to the function, so the internal access and modification are also made to this copy.

Go language can use variadic parameters. Sometimes the number of parameters cannot be determined, so variadic parameters can be used. You can use the ARGS...TYPE format in the parameter section of the function definition statement. This will save all the parameters represented by ... into a slice named ARGS, and note that the data types of these parameters are all TYPE.

Example of Go language function parameters#

Go language parameter passing

// Formal parameter list
func f1(a int, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func main() {
	// Actual parameter list
	r := f1(1, 2)
	fmt.Printf("r: %v\n", r)
}

Demonstrating parameter passing, passing by value

func f1(a int) {
	a = 200
	fmt.Printf("a1: %v\n", a)
}

func main() {
	a := 100
	f1(a)
	fmt.Printf("a: %v\n", a)
}

Output:

a1: 200
a: 100

From the output, it can be seen that after calling the function f1, the value of a is not changed, indicating that the parameter passing is a copy of the original content for computation.

map, slice, interface, channel, and other data types are themselves pointers, so even if they are passed by copy, they are copies of pointers, and the copied parameters still point to the underlying data structure, so modifying them may affect the value of the external data structure.

package main

import "fmt"

func f1(a []int) {
	a[0] = 100
}

func main() {
	a := []int{1, 2}
	f1(a)
	fmt.Printf("a: %v\n", a)
}

Output:

a: [1 2]
a: [100 2]

From the output, it can be seen that the contents of the slice are changed after calling the function.

Variadic parameters

package main

import "fmt"

func f1(args ...int) {
	for _, v := range args {
		fmt.Printf("v: %v\n", v)
	}
}
func f2(name string, age int, args ...int) {
	fmt.Printf("name: %v\n", name)
	fmt.Printf("age: %v\n", age)
	for _, v := range args {
		fmt.Printf("v: %v\n", v)
	}
}
func main() {
	f1(1, 2, 3)
	fmt.Println("------------")
	f1(1, 2, 3, 4, 5, 6)
	fmt.Println("------------")
	f2("tom", 20, 1, 2, 3)
}

Output:

v: 1
v: 2
v: 3
------------
v: 1
v: 2
v: 3
v: 4
v: 5
v: 6
------------
name: tom
age: 20
v: 1
v: 2
v: 3
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.