Skip to content

Instantly share code, notes, and snippets.

View wesleymatosdev's full-sized avatar

wesleymatosdev

View GitHub Profile
// 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() {
// Go supports _constants_ of character, string, boolean,
// and numeric values.
package main
import (
"fmt"
)
// `const` declares a constant value.
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)
for i := 0; i <= 10; i++ {
fmt.Println(i)
}
i := 0
for i <= 10 {
fmt.Println(i)
i += 1
}
for {
fmt.Println("Infinite loop!")
break // use the break keyword to stop your infinite loop
}
// `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.
if 15%2 === 0 {
fmt.Println("15 is even")
} else {
fmt.Println("15 is odd")
}
// Branching with `if` and `else` in Go is
// straight-forward.
package main
import "fmt"
func main() {
// Here's a basic example.
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)
}