Go
Hello World
package main
import "fmt"
func main() {
fmt.Printf("Hello World")
}
Hello World
Functions
package main
import "fmt"
func add(x, y int) int {
return x + y
}
func main() {
fmt.Println(add(42, 13))
}
package main
import "fmt"
func add(x int, y int) int {
return x + y
}
func main() {
fmt.Println(add(42, 13))
}
Hello World
Functions ( cont. )
package main
import "fmt"
func swap(x, y string) (string, string) {
return y, x
}
func main() {
a, b := swap("hello", "world")
fmt.Println(a, b)
}
Hello World
Named return values
package main
import "fmt"
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return
}
func main() {
fmt.Println(split(17))
}
Hello World
Variables
package main
import "fmt"
var x, y int = 1, 2
func main() {
var c, python, java = true, false, "pls no!"
fmt.Println(x, y, c, python, java)
}
Hello World
Variables ( cont. )
package main
import "fmt"
func main() {
c, python, java := true, false, "pls no!"
var x, y int = 1, 2
k := 3
fmt.Println(x, y, k, c, python, java)
}
package main
import "fmt"
var (
c bool = true
python bool = false
java string = "pls no!"
)
func main() {
var x, y int = 1, 2
k := 3
fmt.Println(x, y, k, c, python, java)
}
Hello World
Basic Types
bool
string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64
byte // alias for uint8
rune // alias for int32 ( unicode )
float32 float64
complex64 complex128
Hello World
Default Values
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)
}
0 0 false ""
Hello World
For
package main
import "fmt"
func main() {
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
fmt.Println(sum)
}
Hello World
For ( cont.)
package main
import "fmt"
func main() {
sum := 1
for ; sum < 1000; {
sum += sum
}
fmt.Println(sum)
}
Hello World
"While" is spelt "For" in go.
package main
import "fmt"
func main() {
sum := 1
for sum < 1000 {
sum += sum
}
fmt.Println(sum)
}
Hello World
Forever
package main
func main() {
for {
}
}
Hello World
If
package main
import "fmt"
func add(x, y int) int {
if x < 4 {
return x + y
} else {
return x - y
}
}
Hello World
Switch
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Print("Go runs on ")
os := runtime.GOOS
switch os {
case "darwin":
fmt.Println("OS X.")
case "linux":
fmt.Println("Linux.")
fmt.Printf("%s.", os)
}
}
Hello World
Variadic functions
func sum(nums ...int) {
fmt.Print(nums, " ")
total := 0
for _, num := range nums {
total += num
}
fmt.Println(total)
}
Channels
typed pipes
Send v to channel ch
ch <- v
Receive from ch and assign to v
v := <-ch
Practical (ish) Example
package main
import "fmt"
func add(x, y int, ch chan int) {
ch <- x + y
}
func main() {
ch := make(chan int)
go add(5, 6, ch)
go add(15, 3, ch)
q, z := <- ch, <- ch
fmt.Println(q, z, q+z)
}
Buffered Example
package main
import "fmt"
func main() {
messages := make(chan string, 2)
// Because this channel is buffered, we can send these
// values into the channel without a corresponding
// concurrent receive.
messages <- "buffered"
messages <- "channel"
// Later we can receive these two values as usual.
fmt.Println(<-messages)
fmt.Println(<-messages)
}
Synchonization
package main
import "fmt"
import "time"
func worker(done chan bool) {
fmt.Print("working...")
time.Sleep(time.Second)
fmt.Println("done")
// Send a value to notify that we're done.
done <- true
}
func main() {
// Start a worker goroutine, giving it the channel to
// notify on.
done := make(chan bool, 1)
go worker(done)
// Block until we receive a notification from the
// worker on the channel.
<-done
}
Range
package main
import (
"fmt"
)
func fibonacci(n int, c chan int) {
x, y := 0, 1
for i := 0; i < n; i++ {
c <- x
x, y = y, x+y
}
close(c)
}
func main() {
c := make(chan int, 10)
go fibonacci(15, c)
for i := range c {
fmt.Println(i)
}
}
Go
By ..
Go
- 1,905