当前位置:网站首页>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)}
边栏推荐
- Observer pattern
- clickhouse 思维导图
- Bugs encountered in remote control projects
- Oracle最后一个商用免费版本JDK1.8.202
- 【图像增强】基于Step和Polynomial 滤波实现图像增强附matlab代码
- qps tps rps 区别
- When the centralized platform is gone, everything derived from this platform will be in vain
- 最新豆瓣top250爬虫案例代码分析[注释齐全]
- Go-7-RESTful API的设计
- EfficientNet v2网络学习记录--更小更快
猜你喜欢
全文翻译:欧盟第29条数据保护工作组 数据保护官指南
LeetCode每日一题:搜索插入位置 (均1200道)方法:二分查找
猿辅导联合多方专家共议新课标:语文将更强调“实践性”
经典卷积神经网络ZFNet--解卷积可视化
【图像去噪】基于边缘增强扩散 (cEED) 和 Coherence Enhancing Diffusion (cCED) 滤波器实现图像去噪附matlab代码
The Best Open Source Web Application Firewall to Protect Your Web Applications
论文笔记:SAITS: SELF-ATTENTION-BASED IMPUTATION FOR TIMESERIES
『Another Redis DeskTop Manager』用了这款Redis可视化工具,分析效率提升12倍
浅谈自定义应用层协议与UDP的报文结构和注意事项
Rollup 编译资源离不开 plugin
随机推荐
Docker redis master-slave replication setup, the container cannot be started?
解决有路由策略的情况下域内NAT不通的问题
VS中如何添加依赖的库
gstreamer 记录
全文翻译:欧盟第29条数据保护工作组 数据保护官指南
数据恢复软件EasyRecovery支持恢复所有类型的文件
日文翻译-在线免费日文翻译软件
Image denoising based on edge enhancement Diffusion 】 (cEED) and Coherence Enhancing coursing together (cCED) filter to realize image denoising matlab code
4-4 Matplotlib库 直方图
网络安全基础-基本dos命令(一)
使用百度EasyDL实现智能垃圾箱
2022年中国全民健身发展白皮书
字符串压缩
睿智的目标检测61——Tensorflow2 Focal loss详解与在YoloV4当中的实现
LVGL简介(基于v8.1-8.2)
Qt中QFile、QByteArray QDataStream和QTextStream区别
PostMan import certificate add certificate
OpenSceneGraph3.5.1编译
425 Can‘t open data connection for transfer of “/“
进程和线程