当前位置:网站首页>03、GO语言变量定义、函数
03、GO语言变量定义、函数
2022-08-01 22:04:00 【[email protected]】
目录
1. 第一个Go程序
新建一个文件 example.go
// 声明该文件所在包,如果是主程序就是main,不是主程序就写包名
package main
// 导入库,fmt用来处理标准输入输出
import "fmt"
// main函数就是整个函数的入库,main函数所在的报名也必须为‘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 语言中使用包来组织代码。一般一个文件夹即一个包,包内可以暴露类型
或方法供其他包使用。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 命令
初始化,解决包
# 初始化 go mod init account
# 解决依赖问题(检查,删除错误或者不使用的modules,下载没download的package)
go mod tidy###################################################
3.变量的定义
几种变量定义的方式:
方法1: var b int = 1
1 package main
2
3 import "fmt"
4
5 func main() {
6 // 变量定义方法1
7 // 如果没有给变量赋值,那么变量会获得与数据类型对应的一个默认值
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
// 如果没有给变量赋值,那么变量会获得与数据类型对应的一个默认值
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
注意,不能定义了变量又不使用,否则会报错
[[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
// 如果没有给变量赋值,那么变量会获得与数据类型对应的一个默认值
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
// 如果没有给变量赋值,那么变量会获得与数据类型对应的一个默认值
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
// 指针里面是用来存放地址的
p = &msg
fmt.Println(p)
// 修改指针指向对象的值
*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:参数列表,参数就像⼀个占位符,当函数被调⽤时,你可以将值传递给参数,这个值被称为
实际参数。参数列表指定的是参数类型、顺序、及参数个数。参数是可选的,也就是说函数也可以不包含参
数。
return_types:返回类型,函数返回⼀列值。return_types 是该列值的数据类型。有些功能不需要返回值,
函数体
函数参数类型 函数内对形参操作, 是否影响实参?
变量 否
指针 是
数组 否
数组元素 否
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 调用参数是默认传值过去
fmt.Println(num)
// realadd 传过去的是一个指针,指针指向的是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://blog.csdn.net/qq_48391148/article/details/126109756
边栏推荐
- 程序员必备的 “ 摸鱼神器 ” 来了 !
- Spark practice questions + answers
- The difference between groupByKey and reduceBykey
- Based on php film and television information website management system acquisition (php graduation design)
- Unity Shader 常规光照模型代码整理
- Implementation principle of VGUgarbage collector (garbage collector)
- C语言必杀技3行代码把运行速度提升4倍
- 深度学习Course2第一周Practical aspects of Deep Learning习题整理
- _ _ determinant of a matrix is higher algebra eigenvalue of the product, the characteristic value of matrix trace is combined
- [Mobile Web] Mobile terminal adaptation
猜你喜欢

AI应用第一课:支付宝刷脸登录

模拟数据之mockjs

Based on php online learning platform management system acquisition (php graduation design)

Getting Started Database Days4

Recycling rental system 100% open source without encryption Mall + recycling + rental

MySQL相关知识

【Verilog刷题篇】硬件工程师从0到入门1|基础语法入门

Delicious this year

力扣第 304 场周赛复盘

Still struggling with reporting tool selection?To take a look at this
随机推荐
罗克韦尔AB PLC RSLogix5000中的比较指令使用方法介绍
力扣第 304 场周赛复盘
ARFoundation Getting Started Tutorial U2-AR Scene Screenshot Screenshot
HCIP---Multiple Spanning Tree Protocol related knowledge points
Spark shuffle tuning
第一讲 测试知多少
Homework 8.1 Orphans and Zombies
易周金融分析 | 银行ATM机智能化改造提速;互联网贷款新规带来挑战
xctf攻防世界 Web高手进阶区 web2
dvwa 通关记录1 - 暴力破解 Brute Force
9. SAP ABAP OData 服务如何支持删除(Delete)操作
深度学习Course2第一周Practical aspects of Deep Learning习题整理
selenium无头,防检测
Analysis of the development trend of game metaverse
Dichotomy Medium LeetCode6133. Maximum Number of Groups
PAM 回文自动机
46.全排列
long investment career
render-props and higher order components
【ASM】字节码操作 MethodWriter