go version
得到 go version go1.xx.x linunx/amd64
pwd
得到 /workspaces/oh-my-env
mkdir hello; code hello
得到新窗口,关闭旧窗口go mod init github.com/frankfang/hello
用于初始化目录
如果没有看到 go.mod
,点击 Refresh Explorer
main.go
go build . && ./hello
你也可以用 go run .
代替这两句话创建 main.go
println
函数不常用,可以用 fmt.Println
, 输入 fmt.pl
即可补全
运行 go run .
编译到 Windows
命令
:=
反而不能指定了类型, 但是用的人更多
自己测试
猜猜结果
iota
即希腊字母 ι
iota
是其英文发音GoLang
中 iota
默认为 0
,每行加 1
iota
可以减少 hard code
(写死的代码)const ()
中const
都会重置跟 JS
不一样的地方
( )
,加了会被删for
接 0
个表达式等价于 JS
的 while(true)
for
接 1
个表达式等价于 JS
的 while(condition)
for
接 3
个表达式等价于 JS
的 for
(初始化, 判断, 后续)跟 JS
不一样的地方
if
接一个表达式等价于 JS
的 if(condition)
if
接两个表达式没有等价的 JS
常见写法与 JS
不一样的地方
break
case
可以有多个值,用逗号隔开自己练习
与 JS/TS
不一样的地方
return
可缩写
- numbers ...int
表示多个 int
参数组成的数组Hack
简单值类型
int32
、float64
复杂值类型
struct
)[3]int
引用类型
*int
[]int
map[string]int
chan int
interface {}
自己练习
实在要改的话可以这样改
用于类型
var a *int
表示 a
存 int
的地址a
为指针用于值
&b
表示 b
的地址*c
表示指针 c
对应的值讲师: 我个人认为,用 var a &int 表示 a 存 int 的地址,更符合语义
与 JS
的不同之处
JS
的对象进行类比Go
只支持传值,不过可以把地址当做值 modify(&p1)
label
,用于各种功能;JS
没有 label
自己练习
不怎么用到数组, 不怎么关注就行了
与 JS
的不同之处
JS
的数组进行类比len(a)
获取数组的长度,而不是 a.length