This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // In Go, _variables_ are explicitly declared and used by | |
| // the compiler to e.g. check type-correctness of function | |
| // calls. | |
| package main | |
| import "fmt" | |
| func main() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Go supports _constants_ of character, string, boolean, | |
| // and numeric values. | |
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| // `const` declares a constant value. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "fmt" | |
| func main() { | |
| var i int | |
| var f float64 | |
| var b bool | |
| var s string | |
| fmt.Printf("%v %v %v %q\n", i, f, b, s) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for i := 0; i <= 10; i++ { | |
| fmt.Println(i) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| i := 0 | |
| for i <= 10 { | |
| fmt.Println(i) | |
| i += 1 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for { | |
| fmt.Println("Infinite loop!") | |
| break // use the break keyword to stop your infinite loop | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // `for` is Go's only looping construct. Here are | |
| // some basic types of `for` loops. | |
| package main | |
| import "fmt" | |
| func main() { | |
| // The most basic type, with a single condition. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if 15%2 === 0 { | |
| fmt.Println("15 is even") | |
| } else { | |
| fmt.Println("15 is odd") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Branching with `if` and `else` in Go is | |
| // straight-forward. | |
| package main | |
| import "fmt" | |
| func main() { | |
| // Here's a basic example. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fmt.Print("Go is running on ") | |
| switch os := runtime.GOOS; os { | |
| case "darwin": | |
| fmt.Println("OS X.") | |
| case "linux": | |
| fmt.Println("Linux.") | |
| default: | |
| fmt.Printf("%s.\\n", os) | |
| } |
OlderNewer