JS
for(let i=0;i<10;i++){ console.log(i); }
Go
for i := 0; i < 10; i++ { fmt.Println(i) }
In Go, the for's init and post statement are optional, effectively making it also a "while" statement:
for
var i=0; while(i<10){ console.log(i); i++; }
i := 0 for i < 10 { fmt.Println(i) i++ }
['Rick','Morty','Beth','Summer','Jerry'].forEach(function(value,index){ console.log(value + ' at index ' + index); });
for i, v := range []string{"Rick", "Morty", "Beth", "Summer", "Jerry"} { fmt.Printf("%v at index %d", v, i) }
Go's if can contain an init statement, with variables declared scoped only to the if and else blocks.
if
else
if value := getSomeValue(); value < limit { return value } else { return value / 2 }
The switch statement was one of the motivation for writing this document.
Go defaults to break, and fallthrough needed for otherwise.
fallthrough
Javascript defaults to fallthrough, and break needed for otherwise.
break
switch (favorite) { case "yellow": console.log("yellow"); break; case "red": console.log("red"); case "purple": console.log("(and) purple"); default: console.log("white"); }
switch favorite { case "yellow": fmt.Println("yellow") case "red": fmt.Println("red") fallthrough case "purple": fmt.Println("(and) purple") default: fmt.Println("white") }
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8