当前位置:网站首页>Go - 9 - data type - function
Go - 9 - data type - function
2022-08-09 01:32:00 【hahyyy】
Go's data types are divided into four categories
- Base Type
- Number, String, Boolean
- Aggregate Type
- Arrays, structures
- Reference type
- Pointers, slices, maps, functions, pipes
- Interface Type
Variable Definition
The Go language is statically typed, and the variable type must be specified when the variable is declared
One of the significant differences between Go and other languages is that Go's types come after variables
var a int // if not assigned, defaults to 0var b int = 1 // assigned when declaredvar c = 1 // assign when declaredd := 1 // can only be used in function bodymsg := "hello world" // can only be used in function bodyvar e, f int = 1, 2 // declare multiple variables at onceNote:
- Variables need to be defined before use
- := The variable on the left should not have been declared, otherwise it will cause a compilation error
- The compilation will fail without using the variable definition
- go will determine the type based on the value and will automatically confirm the value based on the type
nil zero value
nil is a pre-identifier in the go language, we can use nil directly without declaring it
nil means none, or zero value
The value type of nil must be a pointer, channel, func, interface, map, or slice type
In the go language, if you declare a variable but do not assign a value to it, then the variable will have a default zero value of a type, and each type has a different corresponding zero value
bool -- false
int -- 0
string -- ""
Other defaults are nil
Pointer
The pointer is the address of a certain value, the symbol * is used when the type is defined, and for an existing variable, use & to get the address of the variable
In general, pointers are usually used when passing parameters to functions, or when defining new methods for a type
In the go language, parameters are passed by value. If a pointer is not used, a copy of the parameter will be copied inside the function, and the modification of the parameter will not affect the value of the external variable.Passing will affect external variables
package mainimport "fmt"func main(){// pointervar p *stringp = &msgfmt.Println(p)// modify the value of the object pointed to by the pointer*p = "Hello"fmt.Println(*p, msg)num := 100add(num)fmt.Println(num)realAdd(&num)fmt.Println(num)}func add(num int){num += 1}func realAdd(num *int){*num += 1}# The returned nums are 100 101Function Definition
The function definition format of go language is as follows
func function_name([parameter list]) [return_types] {function body}Function definition analysis:
- func: The function is declared starting from func
- function_name: The function name, parameter list and return type form the function signature
- parameter list: The parameter list, the parameter is like a placeholder, when the function is called, the value can be passed to the parameter, this value is called the actual parameter.The parameter list specifies the parameter type, order, and number of parameters.Arguments are optional, meaning the function can also have no arguments
- return_types: The return type, the function returns a list of values.return_types is the data type of the column value.Some functions do not require a return value, in which case return_types are not required
- Function body: the code collection of function definitions
| function parameter type | Whether the operation on the formal parameter in the function affects the actual parameter |
| Variable | No |
| Pointer | Yes |
| Array | No |
| Array elements | No |
| slice | Yes |
Parameters and return values
A typical function, using the keyword func, can have multiple parameters and multiple return values
- Functions with no return value
- For functions with no return value, return is not required and cannot be performed in the function body
- Add and divide 2 numbers
- The number and type of return values must be consistent with the declaration
package mainimport "fmt"func main() {fmt.Println(add2(1,2))fmt.Println(add3(3,1))}func add2(num1 int, num2 int) (int, int) {return num1 + num2 , 100}func add3(num1 int, num2 int) (ans int, ans2 int) {ans = num1 + num2ans2 = num1 - num2return}- Implementation: Variable length parameters
package mainimport "fmt"func main() {add4(1,2,3,4,5)}func add4(nums ... int){fmt.Println(reflect.TypeOf(nums))fmt.Println(len(nums))fmt.Println(nums)}边栏推荐
猜你喜欢
随机推荐
4-10 Matplotlib 多图布局
A double non-programmer interviewed Ant, Meituan, Ctrip and other big companies with offers to share the interview process
OpenSceneGraph3.5.1编译
全文翻译:EDPB数据保护影响评估(DPIA:Data Protection Impact Assessment)指南
typescript91-添加任务基本实现
PostMan导入证书 添加证书
Proe/Creo智能硬件产品结构设计要点「干货分享」
ffplay playback control
【信号去噪】基于Sage-Husa自适应卡尔曼滤波器实现海浪磁场噪声抑制及海浪磁场噪声的产生附matlab代码
企业里Foxmail邮箱问题解决方法汇总
typescript90-使用类型文件声明类型
Go-8-Gin框架
观察者模式
OIDC 思维导图
轻量级神经网络SqueezeNext--考虑硬件提速
makefile file compilation
数据恢复软件EasyRecovery支持恢复所有类型的文件
4-2 Matplotlib库 基本使用(绘制折线图)
Qt中QFile、QByteArray QDataStream和QTextStream区别
【图像增强】基于Step和Polynomial 滤波实现图像增强附matlab代码









