当前位置:网站首页>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 once
Note:
- 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 101
Function 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)}
边栏推荐
猜你喜欢
随机推荐
【物理应用】基于El-centro地震波作用下隔震与非隔震支座下的顶层位移、速度、加速度的对比情况附matlab代码
Proe/Creo智能硬件产品结构设计要点「干货分享」
torchversion.transforms的使用
ABAP 报表中如何以二进制方式上传本地文件
全文翻译:欧盟第29条数据保护工作组 数据保护官指南
4-5 Matplotlib库 散点图
【图像去噪】基于边缘增强扩散 (cEED) 和 Coherence Enhancing Diffusion (cCED) 滤波器实现图像去噪附matlab代码
保护您的 Web 应用程序的最佳开源 Web 应用程序防火墙
4-3 Matplotlib库 条形图
ONNX是什么?怎么用?[简明解读版]
C语言-大端存储和小端存储
如何准备一份简历
5-4 Seaborn 线性回归绘图
Introduction to LVGL (based on v8.1-8.2)
Wireshark抓包工具
Go-10-模块与包
JSON basics, transfer JSON data, and introduce four mainstream frameworks, jackson, gson, fastjson, and json-lib!
makefile file compilation
软件测试的调用接口怎么调用,逻辑是什么?
The Best Open Source Web Application Firewall to Protect Your Web Applications