当前位置:网站首页>A small test of basic grammar, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, basic grammar of go lang and the use of variables EP02
A small test of basic grammar, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, basic grammar of go lang and the use of variables EP02
2022-08-05 10:18:00 【InfoQ】
变量的声明与使用
package main // 声明 main 包
import f "fmt" // 导入 fmt 包,打印字符串时需要用到
func main() { // 声明 main 主函数入口
//声明变量
var name int
name = 1
f.Println(name)
}
1
package main // 声明 main 包
import f "fmt" // 导入 fmt 包,打印字符串时需要用到
func main() { // 声明 main 主函数入口
//声明变量
var name int
f.Println(name)
}
0
package main // 声明 main 包
import f "fmt" // 导入 fmt 包,打印字符串时需要用到
func main() { // 声明 main 主函数入口
//声明变量
var name = 1
f.Println(name)
}
package main // 声明 main 包
import f "fmt" // 导入 fmt 包,打印字符串时需要用到
func main() { // 声明 main 主函数入口
//声明变量
name := 1
f.Println(name)
}
package main // 声明 main 包
import f "fmt" // 导入 fmt 包,打印字符串时需要用到
func main() { // 声明 main 主函数入口
//声明变量
name = 1
//重复赋值
name := 2
f.Println(name)
}
command-line-arguments
# command-line-arguments
.\test.go:9:2: undefined: name
> Elapsed: 1.097s
> Result: Error
package main // 声明 main 包
import f "fmt" // 导入 fmt 包,打印字符串时需要用到
func main() { // 声明 main 主函数入口
//声明变量
var name1, name2, name3 = 1, "2", false
f.Println(name1)
f.Println(name2)
f.Println(name3)
}
1
2
false
package main // 声明 main 包
import f "fmt" // 导入 fmt 包,打印字符串时需要用到
func main() { // 声明 main 主函数入口
//声明变量
var name = "你好"
name = 1
f.Println(name)
}
command-line-arguments
# command-line-arguments
.\test.go:11:9: cannot use 1 (untyped int constant) as string value in assignment
> Elapsed: 0.561s
> Result: Error
func main() {
var a string = "abc"
fmt.Println("hello, go lang")
}
a declared and not used
变量内存地址、占用空间大小和交换赋值
package main // 声明 main 包
import f "fmt" // 导入 fmt 包,打印字符串时需要用到
func main() { // 声明 main 主函数入口
//声明变量
var name = "你好"
f.Println("name的内存地址是", &name)
}
name的内存地址是 0xc00003c250
package main // 声明 main 包
import f "fmt" // 导入 fmt 包,打印字符串时需要用到
func main() { // 声明 main 主函数入口
//声明变量
var name = "你好"
a := name
f.Println("name的内存地址是", &name)
f.Println("a的内存地址是", &a)
}
name的内存地址是 0xc00003c230
a的内存地址是 0xc00003c240
package main // 声明 main 包
import f "fmt" // 导入 fmt 包,打印字符串时需要用到
import u "unsafe"
func main() { // 声明 main 主函数入口
a := 100
f.Println("a的地址:", &a)
f.Println("占用内存:", u.Sizeof(a))
}
a的地址: 0xc0000aa058
占用内存: 8
package main // 声明 main 包
import f "fmt" // 导入 fmt 包,打印字符串时需要用到
func main() { // 声明 main 主函数入口
b, a := 1, 2
f.Println(&b, &a)
b, a = a, b
f.Println(b, a)
f.Println(&b, &a)
}
0xc00012c058 0xc00012c070
2 1
0xc00012c058 0xc00012c070
package main // 声明 main 包
import f "fmt" // 导入 fmt 包,打印字符串时需要用到
func main() { // 声明 main 主函数入口
a, _ := 1, 2
f.Println("a = ", a) // 1
}
常量constant
package main // 声明 main 包
import f "fmt" // 导入 fmt 包,打印字符串时需要用到
func main() { // 声明 main 主函数入口
const value int = 1
// value = 100 // 常量是不允许被修改的
f.Println("value = ", value)
}
const (
Unknown = 0
Female = 1
Male = 2
)
package main // 声明 main 包
import f "fmt" // 导入 fmt 包,打印字符串时需要用到
func main() { // 声明 main 主函数入口
// const来定义枚举类型
const (
// 可以在const()中添加一个关键字iota, 每行的iota都会累加1, 第一行的iota默认是0
a = iota + 1 // iota = 0
b // iota = 1
c // iota = 2
)
f.Println("a = ", a) // 1
f.Println("b = ", b) // 2
f.Println("c = ", c) // 3
}
变量作用域
//局部变量
package main
import "fmt"
//主函数
func main() {
//从这里开始主函数的局部作用域
//主函数内的局部变量
var myvariable1, myvariable2 int = 69, 145
// 显示变量的值
fmt.Printf("myvariable1 变量的值 : %d\n", myvariable1)
fmt.Printf("myvariable2 变量的值 : %d\n", myvariable2)
} // 此处主要函数的局部作用域结束
//全局变量
package main
import "fmt"
// 全局变量声明
var myvariable1 int = 100
func main() {
// 主函数内部的局部变量
var myvariable2 int = 200
//显示全局变量
fmt.Printf("全局变量 myvariable1 的值是 : %d\n", myvariable1)
//显示局部变量
fmt.Printf("局部变量 myvariable2 的值是 : %d\n", myvariable2)
//调用函数
display()
}
func display() {
// 显示全局变量
fmt.Printf("全局变量 myvariable1 的值是 : %d\n", myvariable1)
}
var和const :变量和常量的声明
var varName type 或者 varName : = value
package and import: 导入
func: 用于定义函数和方法
return :用于从函数返回
defer someCode :在函数退出之前执行
go : 用于并行
select 用于选择不同类型的通讯
interface 用于定义接口
struct 用于定义抽象数据类型
break、case、continue、for、fallthrough、else、if、switch、goto、default 流程控制
chan用于channel通讯
type用于声明自定义类型
map用于声明map类型数据
range用于读取slice、map、channel数据
结语
边栏推荐
- Custom filters and interceptors implement ThreadLocal thread closure
- Brief Analysis of WSGI Protocol
- 数据中台建设(十):数据安全管理
- 上位机开发C#语言:模拟STC串口助手接收单片机发送数据
- 牛刀小试基本语法,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang基本语法和变量的使用EP02
- Voice-based social software development - making the most of its value
- NowCoderTOP35-40——持续更新ing
- Score interview (1)----related to business
- SD NAND Flash简介!
- Analysis and practice of antjian webshell dynamic encrypted connection
猜你喜欢
RT-Thread记录(一、RT-Thread 版本、RT-Thread Studio开发环境 及 配合CubeMX开发快速上手)
数据中台建设(十):数据安全管理
Voice-based social software development - making the most of its value
three.js debugging tool dat.gui use
leetcode: 529. Minesweeper Game
还在找网盘资源吗?快点收藏如下几个值得收藏的网盘资源搜索神器吧!
Complete image segmentation efficiently based on MindSpore and realize Dice!
MySQL transactions
Which big guy has the 11G GI and ojvm patches in April or January 2020, please help?
Getting started with Polkadot parachain development, this article is enough
随机推荐
百年北欧奢华家电品牌ASKO智能三温区酒柜臻献七夕,共品珍馐爱意
three.js调试工具dat.gui使用
深入理解 Istio 流量管理的超时时间设置
The query that the user's test score is greater than the average score of a single subject
How can project cost control help project success?
Oracle temporary table space role
项目成本控制如何帮助项目成功?
数据中台建设(十):数据安全管理
This notebook of concurrent programming knowledge points strongly recommended by Ali will be a breakthrough for you to get an offer from a big factory
语音社交软件开发——充分发挥其价值
How does the official account operate and maintain?Public account operation and maintenance professional team
告白数字化转型时代:麦聪软件以最简单的方式让企业把数据用起来
What is CRM Decision Analysis Management?
leetcode: 529. Minesweeper Game
ffmpeg drawtext add text watermark
js hijacks the array push method
电竞、便捷、高效、安全,盘点OriginOS功能的关键词
Oracle 19.3 restart 环境
电气工程的标准是什么
【MindSpore易点通机器人-01】你也许见过很多知识问答机器人,但这个有点不一样