JavaScriptJavaScriptGolangGolang
变量声明
JavaScriptvarlet
{
let a = 10
var b = 1
}
Golangvar
var a = 10
Golang
var a int = 10
:=
func main() {
a := 10
}
GolangJavaScript
const a = 10
条件语句
JavaScriptC
let a = 10
if (a > 0) {
console.log(a)
}
Golang;if
if a := 10; a > 0 {
fmt.Println(a)
}
switch
JavaScriptcasebreak
switch (fruit) {
case "banana":
// ...
break
case "apple":
// ...
break
default:
// ...
}
Golangcasebreak
switch fruit {
case "banana":
// ...
case "apple":
// ...
default:
// ...
}
fallthrough
循环语句
JavaScriptwhiledo whileforfor infor of
Golangfor
for i := 0; i < 100; i++ {
fmt.Println(i)
}
for
for {
// ...
}
函数
JavaScriptfunction
GolangfuncJavaScript
func createIncrementor(start int) func() int {
return func () int {
start++
return start;
}
}
func main() {
inc := createIncrementor(5);
inc() // 5
inc() // 6
inc() // 7
}
欢迎关注我的公众号“野生程序员的修炼”,原创技术文章第一时间推送。