search
HomeBackend DevelopmentGolangWhat are the requirements for golang formal parameters?

The parameters of functions in the Go language are formal parameters, which are local variables whose values ​​are provided by the parameter caller; if the types of adjacent formal parameters are the same, the types of the first few parameters can be omitted. , you only need to write the type of the last parameter, the syntax "func funcName(formal parameter 1, formal parameter 2 parameter type 1, formal parameter 3, formal parameter 4 parameter type 2, ...) (return value list) { / /Execute statement...return return value list}".

What are the requirements for golang formal parameters?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Go language function parameters

The parameters of functions in Go language are formal parameters, that is, if we pass by value , the parameter passed is actually a copy of the actual parameter, not the real actual parameter. Formal parameters are local variables whose values ​​are provided by the caller of the parameter.

In the Go language, if the types of adjacent function parameters are the same, then we can omit the types of the first few parameters and only need to write the type of the last parameter.

func funcName(param1, param2 paramType1, param3, param4 paramType2, ...)(returnVal returnType){	// 执行语句...
	return 返回值列表
}

Explanation

  • The parameters param1 and parameter param2 have the same type, then we can omit the parameter type after param1 and put it directly after param2 Write type.

  • The parameters param3 and parameter param4 have the same type, so we can omit the parameter type after param3 and write the type directly after param4.

  • returnVal returnType is the return value list, describing the variable name and type of the function return value. If the function returns an unnamed variable or no return value, the return value list The parentheses can be omitted.

Case

Use functions to find the greatest common divisor of two numbers

package main
import (
	"fmt"
)
func gcdNormal(x, y int) int {
	var n int
	if x > y {
		n = y
	} else {
		n = x
	}
	for i := n; i >= 1; i-- {
		if x%i == 0 && y%i == 0 {
			return i
		}
	}
	return 1
}
func main() {
	//用函数,实现计算两个数的最大公约数
	gcd := gcdNormal(10,20)
	fmt.Println("Gcd =", gcd)
}

After the program runs, the console output is as follows:

What are the requirements for golang formal parameters?

We define a function gcdNormal, passing in two int type parameters a and b, the function Returns a variable of type int. This function uses the exhaustive method to implement the logic of finding the greatest common divisor of parameter a and parameter b. Because the types of parameter a and parameter b are both int types, we omit the type after parameter a and write it directly in parameter b. later.

In the main() function, we called the gcdNormal function and passed in the two parameters 10 and 20. We used the gcd variable to accept the value returned by the gcdNormal function, and finally used the Println() function to print the final result.

Function parameters are formal parameters

The function parameters of the Go language are formal parameters, and modifications to the function parameters will not affect the actual parameters

package main
import (
"fmt"
)
func change(a int) {
a += 100
}
func main() {
//Go语言的函数参数是函数参数,对函数参数的修改不会影响实参
a := 10
change(a)
fmt.Println("a =", a)
}

Program After running, the console output is as follows:

What are the requirements for golang formal parameters?

We define a function change() and pass in an int type parameter a. In the function body, for The parameters of this function implement the operation of adding 100.

In the main() function, call this function and pass in the variable a. After executing the change() function, we print the value of the variable a again and find that the value of a is still 10, not 100. 110 after that.

Because in the Go language, the parameters of the function are formal parameters, that is, copies of the actual parameters, so what is modified is not the actual parameters, so the value of a will not change.

Modify actual parameters through function parameters

Go language functions must use pointer types if they want to modify actual parameters through function parameters

package main
import (
	"fmt"
)
func change(a *int) {
	*a += 100
}
func main() {
	//Go语言的函数要通过函数参数修改实参,必须要使用指针类型
	a := 10
	change(&a)
	fmt.Println("a =", a)
}

What are the requirements for golang formal parameters?

We define a function change(), passing in a parameter a of int pointer type, and in the function body, add 100 to the parameter of the function.

In the main() function, call this function and pass in the address of variable a. After executing the change() function, we print the value of variable a again and find that the value of a has been increased by 100. It became 110.

The change() function here passes in the address of a. Then we use the pointer operator in the change() function to obtain the variable pointed to by the address of a, and then modify its value. In fact, the modification is the value of variable a itself.

Summary of Go language function parameters

The parameters of functions in Go language are formal parameters, that is, if we pass by value, The parameters are actually a copy of the actual parameters, not the real actual parameters.

In the Go language, if the types of adjacent function parameters are the same, then we can omit the types of the first few parameters and only need to write the type of the last parameter.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of What are the requirements for golang formal parameters?. 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
go语言有没有缩进go语言有没有缩进Dec 01, 2022 pm 06:54 PM

go语言有缩进。在go语言中,缩进直接使用gofmt工具格式化即可(gofmt使用tab进行缩进);gofmt工具会以标准样式的缩进和垂直对齐方式对源代码进行格式化,甚至必要情况下注释也会重新格式化。

go语言为什么叫gogo语言为什么叫goNov 28, 2022 pm 06:19 PM

go语言叫go的原因:想表达这门语言的运行速度、开发速度、学习速度(develop)都像gopher一样快。gopher是一种生活在加拿大的小动物,go的吉祥物就是这个小动物,它的中文名叫做囊地鼠,它们最大的特点就是挖洞速度特别快,当然可能不止是挖洞啦。

一文详解Go中的并发【20 张动图演示】一文详解Go中的并发【20 张动图演示】Sep 08, 2022 am 10:48 AM

Go语言中各种并发模式看起来是怎样的?下面本篇文章就通过20 张动图为你演示 Go 并发,希望对大家有所帮助!

【整理分享】一些GO面试题(附答案解析)【整理分享】一些GO面试题(附答案解析)Oct 25, 2022 am 10:45 AM

本篇文章给大家整理分享一些GO面试题集锦快答,希望对大家有所帮助!

tidb是go语言么tidb是go语言么Dec 02, 2022 pm 06:24 PM

是,TiDB采用go语言编写。TiDB是一个分布式NewSQL数据库;它支持水平弹性扩展、ACID事务、标准SQL、MySQL语法和MySQL协议,具有数据强一致的高可用特性。TiDB架构中的PD储存了集群的元信息,如key在哪个TiKV节点;PD还负责集群的负载均衡以及数据分片等。PD通过内嵌etcd来支持数据分布和容错;PD采用go语言编写。

go语言是否需要编译go语言是否需要编译Dec 01, 2022 pm 07:06 PM

go语言需要编译。Go语言是编译型的静态语言,是一门需要编译才能运行的编程语言,也就说Go语言程序在运行之前需要通过编译器生成二进制机器码(二进制的可执行文件),随后二进制文件才能在目标机器上运行。

go语言能不能编译go语言能不能编译Dec 09, 2022 pm 06:20 PM

go语言能编译。Go语言是编译型的静态语言,是一门需要编译才能运行的编程语言。对Go语言程序进行编译的命令有两种:1、“go build”命令,可以将Go语言程序代码编译成二进制的可执行文件,但该二进制文件需要手动运行;2、“go run”命令,会在编译后直接运行Go语言程序,编译过程中会产生一个临时文件,但不会生成可执行文件。

golang map怎么删除元素golang map怎么删除元素Dec 08, 2022 pm 06:26 PM

删除map元素的两种方法:1、使用delete()函数从map中删除指定键值对,语法“delete(map, 键名)”;2、重新创建一个新的map对象,可以清空map中的所有元素,语法“var mapname map[keytype]valuetype”。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function