当前位置:网站首页>03. GO language variable definition, function
03. GO language variable definition, function
2022-08-01 22:07:00 【[email protected]】
目录
1. 第一个Go程序
新建一个文件 example.go
// 声明该文件所在包,如果是主程序就是main,Write the package name instead of the main program
package main
// 导入库,fmt用来处理标准输入输出
import "fmt"
// mainA function is a repository for an entire function,mainThe registration where the function is located must also be ‘main’
func main() {
// 调用fmt的Println方法,在屏幕输出信息
fmt.Println("hello,world")
fmt.Println("this is my first Go!")
}
运行结果
[[email protected] go_lianxi]# go run example.go
hello,world
this is my first Go!
Go语法规则:
package main
声明了 main.go 所在的包,Go 语言中使用包来组织代码.一般一个文件夹即一个包,Types can be exposed within a package
or methods for use by other packages.import “fmt”
fmt 是 Go 语言的一个标准库/包,用来处理标准输入输出.func main
main 函数是整个程序的入口,main 函数所在的包名也必须为 main .
!!主包中必须包含一个main函数!!fmt.Println(“Hello World!”)
调用 fmt 包的 Println 方法,打印出 “Hello World!”其他知识点:
一行代表一个语句结束,多个语句写在同一行,它们则必须使用 ; 人为区分
注释:
单行注释: //
多行注释:/* sth */
标识符:同python标识符规则先编译再执行

2. Go语言命令
go build命令
[[email protected] go_lianxi]# ls
example.go
[[email protected] go_lianxi]# go build example.go
[[email protected] go_lianxi]# ls
example example.go
[[email protected] go_lianxi]# ./example
hello,world
this is my first Go!
[[email protected] go_lianxi]# ll
总用量 1724
-rwxr-xr-x. 1 root root 1758476 8月 1 20:55 example
-rw-r--r--. 1 root root 391 8月 1 20:51 example.go
###################################################
go run 命令
运行源代码
[[email protected] go_lianxi]# go run example.go
hello,world
this is my first Go!
###################################################
go mod init 命令
初始化,solution package
# 初始化 go mod init account
# 解决依赖问题(检查,删除错误或者不使用的modules,下载没download的package)
go mod tidy###################################################
3.变量的定义
Several ways to define variables:
方法1: var b int = 1
1 package main
2
3 import "fmt"
4
5 func main() {
6 // 变量定义方法1
7 // 如果没有给变量赋值,The variable then gets a default value corresponding to the data type
8 var a int
9 var b int = 1
10 var s1 string = "b"
11 fmt.Println(a,b,s1)
12 } 执行结果:
[[email protected] go_lianxi]# vim var.go
[[email protected] go_lianxi]# go run var.go
0 1 b
###################################################
方法2 var := 4
[[email protected] go_lianxi]# cat var.go
package main
import "fmt"
func main() {
// 变量定义方法1
// 如果没有给变量赋值,The variable then gets a default value corresponding to the data type
var a int
var b int = 1
var s1 string = "b"
fmt.Println(a,b,s1)
// 方法2
c := 4
msg := "msg"
fmt.Println(c,msg)
}
执行结果
[[email protected] go_lianxi]# go run var.go
0 1 b
4 msg
注意,A variable cannot be defined and not used,否则会报错
[[email protected] go_lianxi]# go run var.go
# command-line-arguments
./var.go:10:6: s declared but not used
./var.go:11:18: undefined: s1
###################################################
查看变量类型
使用reflect库
package main
import "fmt"
import "reflect"
func main() {
// 变量定义方法1
// 如果没有给变量赋值,The variable then gets a default value corresponding to the data type
var a int
var b int = 1
var s1 string = "b"
fmt.Println(a,b,s1)
// 方法2
c := 4
msg := "msg"
fmt.Println(c,msg)
d := 3.14159
fmt.Println(reflect.TypeOf(b))
fmt.Println(reflect.TypeOf(s1))
fmt.Println(reflect.TypeOf(d))
}
执行结果
[[email protected] go_lianxi]# go run var.go
0 1 b
4 msg
int
string
float64
###################################################
4.Go里面的指针
package main
import "fmt"
import "reflect"
func main() {
// 变量定义方法1
// 如果没有给变量赋值,The variable then gets a default value corresponding to the data type
var a int
var b int = 1
var s1 string = "b"
fmt.Println(a,b,s1)
// 方法2
c := 4
msg := "msg"
fmt.Println(c,msg)
d := 3.14159
fmt.Println(reflect.TypeOf(b))
fmt.Println(reflect.TypeOf(s1))
fmt.Println(reflect.TypeOf(d))
var p *string
// Pointers are used to store addresses
p = &msg
fmt.Println(p)
// Modify the value of the object pointed to by the pointer
*p = "hello"
fmt.Println(*p)
fmt.Println(msg)
}
执行结果
[[email protected] go_lianxi]# go run var.go
0 1 b
4 msg
int
string
float64
0xc000010260
hello
hello
###################################################
5.Go语言的函数(functions)
func function_name( [parameter list] ) [return_types] {
函数体
}函数定义解析:
func:函数由 func 开始声明
function_name:函数名称,参数列表和返回值类型构成了函数签名.
parameter list:参数列表,Parameters are like⼀个占位符,when the function is called⽤时,你可以将值传递给参数,这个值被称为
实际参数.参数列表指定的是参数类型、顺序、及参数个数.参数是可选的,That is to say, the function can also have no parameters
数.
return_types:返回类型,函数返回⼀列值.return_types 是该列值的数据类型.有些功能不需要返回值,
函数体
函数参数类型 Operations on formal parameters within a function, Whether to affect actual parameters?
变量 否
指针 是
数组 否
数组元素 否
slice 是
这种情况下 return_types 不是必须的.package main
import "fmt"
func funcname() {
fmt.Println("hello world")
}
func main() {
// 调⽤
funcname()
}###################################################
5.5实现2个数的加法、减法等
[[email protected] src]# cat func01.go
package main
import "fmt"
func main() {
fmt.Println("vim-go")
num := 100
add(num)
// go Call parameters are passed by default by default
fmt.Println(num)
// realadd Passed is a pointer,指针指向的是num的地址,
realadd(&num)
fmt.Println(num)
fmt.Println(add2(1,5))
fmt.Println(add3(3,1))
}
func add(num int) {
num +=1
}
func realadd(num *int) {
*num += 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+num2
ans2 = num1-num2
return
}
[[email protected] src]# go run func01.go
vim-go
100
101
6 100
4 2
func add(num1 int, num2 int) (ans int) {
ans = num1 + num2
return
}###################################################
5.6 可变长参数
[[email protected] src]# cat func02.go
package main
import (
"fmt"
"reflect"
)
func main() {
fmt.Println("vim-go")
add(1,2,3,4,5)
}
func add(nums ...int) {
fmt.Println(reflect.TypeOf(nums)) // 输出 []int
fmt.Println(len(nums)) // 输出 5
fmt.Println(nums) // 输出 [1,2,3,4,5]
sum := 0
for i := 0; i < len(nums); i++ {
sum += nums[i] // 输出15
}
fmt.Println(sum)
}
执行结果
[[email protected] src]# go run func02.go
vim-go
[]int
5
[1 2 3 4 5]
15
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/213/202208012204214967.html
边栏推荐
- LeetCode952三部曲之二:小幅度优化(137ms -> 122ms,超39% -> 超51%)
- 高等代数_证明_矩阵的行列式为特征值之积, 矩阵的迹为特征值之和
- 2022-08-01 第八组 曹雨 泛型 枚举
- Spark shuffle tuning
- Centos7--MySQL的安装
- [Mobile Web] Mobile terminal adaptation
- Recycling rental system 100% open source without encryption Mall + recycling + rental
- feel so stupid
- User Experience | How to Measure User Experience?
- Lecture 3: Several common table field data types in MySQL database
猜你喜欢

kubernetes CoreDNS全解析

Based on php online examination management system acquisition (php graduation design)

小程序中的多表联合查询

小程序毕设作品之微信美食菜谱小程序毕业设计成品(5)任务书

【C语言实现】两种计算平均成绩题型,博主精心整理,值得一读

Today's sleep quality record 74 points

不卷了!入职字节跳动一周就果断跑了。

工程建筑行业数据中台指标分析

LeetCode952三部曲之二:小幅度优化(137ms -> 122ms,超39% -> 超51%)

模拟数据之mockjs
随机推荐
小程序毕设作品之微信体育馆预约小程序毕业设计成品(4)开题报告
Flutter基础学习(一)Dart语言入门
【C语言实现】求两个整数的较大值
ImportError: `save_weights` requires h5py.问题解决
漫长的投资生涯
线上故障排查方案
Based on php animation peripheral mall management system (php graduation design)
【牛客刷题-SQL大厂面试真题】NO4.出行场景(某滴打车)
[ASM] Bytecode Operation MethodWriter
ARFoundation Getting Started Tutorial U2-AR Scene Screenshot Screenshot
render-props and higher order components
企业公众号文章写作方向:如何写出读者认可的优质内容
seaborn笔记:可视化统计关系(散点图、折线图)
Postman 批量测试接口详细教程
Spark practice questions + answers
Shell programming conditional statement
The Microsoft campus ambassador to shout you to autumn recruit!
深度学习Course2第二周Optimization Algorithms习题整理
【开源】Sentinel高性能高可用集群限流解决方案
SOM Network 1: Principles Explained