package main
import "fmt"
func main() {
fmt.Printf("Hello World")
}
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))
}
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)
}
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))
}
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)
}
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)
}
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
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 ""
package main
import "fmt"
func main() {
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
fmt.Println(sum)
}
package main
import "fmt"
func main() {
sum := 1
for ; sum < 1000; {
sum += sum
}
fmt.Println(sum)
}
package main
import "fmt"
func main() {
sum := 1
for sum < 1000 {
sum += sum
}
fmt.Println(sum)
}
package main
func main() {
for {
}
}
package main
import "fmt"
func add(x, y int) int {
if x < 4 {
return x + y
} else {
return x - y
}
}
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)
}
}
func sum(nums ...int) {
fmt.Print(nums, " ")
total := 0
for _, num := range nums {
total += num
}
fmt.Println(total)
}
ch <- v
v := <-ch
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)
}
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)
}
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
}
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)
}
}