当前位置:网站首页>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
边栏推荐
- Dichotomy Medium LeetCode6133. Maximum Number of Groups
- shell编程规范与变量
- Implementation principle of VGUgarbage collector (garbage collector)
- shell programming conventions and variables
- 企业公众号文章写作方向:如何写出读者认可的优质内容
- SOM网络1:原理讲解
- 如何防范 DAO 中的治理攻击?
- 第3讲:MySQL数据库中常见的几种表字段数据类型
- 基于 OData 模型和 JSON 模型的 SAP UI5 表格控件行项目的添加和删除实现
- 解决 win10 下 ISE14.7的 iMPACT 崩溃问题 - FPGA 笔记
猜你喜欢

入门数据库Days4

【建议收藏】ヾ(^▽^*)))全网最全输入输出格式符整理

JS prototype hasOwnProperty in 加方法 原型终点 继承 重写父类方法

FusionGAN:A generative adversarial network for infrared and visible image fusion article study notes

【开源】Sentinel高性能高可用集群限流解决方案

小程序毕设作品之微信美食菜谱小程序毕业设计成品(7)中期检查报告

Based on php online music website management system acquisition (php graduation design)

Homework 8.1 Orphans and Zombies

AIDL communication

_ _ determinant of a matrix is higher algebra eigenvalue of the product, the characteristic value of matrix trace is combined
随机推荐
VGUgarbage collector(垃圾回收器)的实现原理
Based on php Xiangxi tourism website management system acquisition (php graduation design)
Postman 批量测试接口详细教程
Port protocol for WEB penetration
【牛客刷题-SQL大厂面试真题】NO4.出行场景(某滴打车)
FusionGAN:A generative adversarial network for infrared and visible image fusion article study notes
教你VSCode如何快速对齐代码、格式化代码
Dichotomy Medium LeetCode6133. Maximum Number of Groups
深度学习Course2第一周Practical aspects of Deep Learning习题整理
求解多元多次方程解的个数
递归(各经典例题分析)
Still struggling with reporting tool selection?To take a look at this
不卷了!入职字节跳动一周就果断跑了。
HCIP---Architecture of Enterprise Network
1. @Component注解的原理剖析
越长大越孤单
感觉自己好傻
线程池分析
今年的很美味
10 Practical Uses of NFTs (NFT System Development)