initial commit
This commit is contained in:
commit
a95155e27d
25
append.go
Normal file
25
append.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var s []int
|
||||
printSlice(s)
|
||||
|
||||
// append works on nil slices
|
||||
s = append(s, 0)
|
||||
printSlice(s)
|
||||
|
||||
// the slice grows as needed
|
||||
s = append(s, 1)
|
||||
printSlice(s)
|
||||
|
||||
// we can add more than one element at a time
|
||||
s = append(s, 2, 3, 4)
|
||||
printSlice(s)
|
||||
|
||||
}
|
||||
|
||||
func printSlice(s []int) {
|
||||
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
|
||||
}
|
14
array.go
Normal file
14
array.go
Normal file
@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var a[2] string
|
||||
a[0] = "Hello"
|
||||
a[1] = "World"
|
||||
fmt.Println(a[0], a[1])
|
||||
fmt.Println(a)
|
||||
|
||||
primes := [6]int{2, 3, 5, 7, 11, 13}
|
||||
fmt.Println(primes)
|
||||
}
|
18
basic-types.go
Normal file
18
basic-types.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/cmplx"
|
||||
)
|
||||
|
||||
var (
|
||||
ToBe bool = false
|
||||
MaxInt uint64 = 1 << 64 - 1
|
||||
z complex128 = cmplx.Sqrt(-5 + 12i)
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Printf("Type: %T Value: %v\n", ToBe, ToBe)
|
||||
fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt)
|
||||
fmt.Printf("Type: %T Value: %v\n", z, z)
|
||||
}
|
14
constants.go
Normal file
14
constants.go
Normal file
@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
const Pi = 3.14
|
||||
|
||||
func main() {
|
||||
const World = "world"
|
||||
fmt.Println("Hello", World)
|
||||
fmt.Println("Hello", Pi, "Day")
|
||||
|
||||
const Truth = true
|
||||
fmt.Println("Go rules?", Truth)
|
||||
}
|
13
defer-multi.go
Normal file
13
defer-multi.go
Normal file
@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main () {
|
||||
fmt.Println("counting")
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
defer fmt.Println(i)
|
||||
}
|
||||
|
||||
fmt.Println("done")
|
||||
}
|
9
defer.go
Normal file
9
defer.go
Normal file
@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
defer fmt.Println("world")
|
||||
|
||||
fmt.Println("hello")
|
||||
}
|
25
exercise-fibonacci-closure.go
Normal file
25
exercise-fibonacci-closure.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// fibonacci is a function that returns
|
||||
// a function that returns an int.
|
||||
func fibonacci() func() int {
|
||||
x := 0
|
||||
y := 1
|
||||
z := 0
|
||||
return func() int {
|
||||
z, x, y = x, y, x + y
|
||||
if z == 0 {
|
||||
return 0
|
||||
}
|
||||
return z
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
f := fibonacci()
|
||||
for i := 0; i < 10; i++ {
|
||||
fmt.Println(f())
|
||||
}
|
||||
}
|
21
exercise-loops-and-functions.go
Normal file
21
exercise-loops-and-functions.go
Normal file
@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func Sqrt(x float64) float64 {
|
||||
var p float64
|
||||
for z := 1.0; z != p; z -= (z * z - x) / (2 * z) {
|
||||
p = z
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Printf("Stdlib: %g, Mine: %g\n", math.Sqrt(333), Sqrt(333))
|
||||
fmt.Printf("Stdlib: %g, Mine: %g\n", math.Sqrt(33), Sqrt(33))
|
||||
fmt.Printf("Stdlib: %g, Mine: %g\n", math.Sqrt(17), Sqrt(17))
|
||||
fmt.Printf("Stdlib: %g, Mine: %g\n", math.Sqrt(4), Sqrt(4))
|
||||
}
|
20
exercise-maps.go
Normal file
20
exercise-maps.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"golang.org/x/tour/wc"
|
||||
)
|
||||
|
||||
var count map[string]int
|
||||
|
||||
func WordCount(s string) map[string]int {
|
||||
count = make(map[string]int)
|
||||
for _, w := range strings.Fields(s) {
|
||||
count[w]++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func main() {
|
||||
wc.Test(WordCount)
|
||||
}
|
18
exercise-slices.go
Normal file
18
exercise-slices.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import "golang.org/x/tour/pic"
|
||||
|
||||
func Pic(dx, dy int) [][]uint8 {
|
||||
pic := make([][]uint8, dy)
|
||||
for y := range pic {
|
||||
pic[y] = make([]uint8, dx)
|
||||
for x := range pic[y] {
|
||||
pic[y][x] = uint8(x) ^ uint8(y)
|
||||
}
|
||||
}
|
||||
return pic
|
||||
}
|
||||
|
||||
func main() {
|
||||
pic.Show(Pic)
|
||||
}
|
10
exported-names.go
Normal file
10
exported-names.go
Normal file
@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(math.Pi)
|
||||
}
|
11
for-continued.go
Normal file
11
for-continued.go
Normal file
@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
sum := 1
|
||||
for sum < 1000 {
|
||||
sum += sum
|
||||
}
|
||||
fmt.Println(sum)
|
||||
}
|
11
for-is-gos-while.go
Normal file
11
for-is-gos-while.go
Normal file
@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
sum := 1
|
||||
for sum < 1000 {
|
||||
sum += sum
|
||||
}
|
||||
fmt.Println(sum)
|
||||
}
|
11
for.go
Normal file
11
for.go
Normal file
@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
sum := 0
|
||||
for i := 0; i < 10; i++ {
|
||||
sum += i
|
||||
}
|
||||
fmt.Println(sum)
|
||||
}
|
6
forever.go
Normal file
6
forever.go
Normal file
@ -0,0 +1,6 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
for {
|
||||
}
|
||||
}
|
21
function-closures.go
Normal file
21
function-closures.go
Normal file
@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func adder() func(int) int {
|
||||
sum := 0
|
||||
return func(x int) int {
|
||||
sum += x
|
||||
return sum
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
pos, neg := adder(), adder()
|
||||
for i := 0; i < 10; i++ {
|
||||
fmt.Println(
|
||||
pos(i),
|
||||
neg(-2*i),
|
||||
)
|
||||
}
|
||||
}
|
20
function-values.go
Normal file
20
function-values.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func compute(fn func(float64, float64) float64) float64 {
|
||||
return fn(3, 4)
|
||||
}
|
||||
|
||||
func main() {
|
||||
hypot := func(x, y float64) float64 {
|
||||
return math.Sqrt(x*x + y*y)
|
||||
}
|
||||
fmt.Println(hypot(5, 12))
|
||||
|
||||
fmt.Println(compute(hypot))
|
||||
fmt.Println(compute(math.Pow))
|
||||
}
|
11
functions-continued.go
Normal file
11
functions-continued.go
Normal file
@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func add(x, y int) int {
|
||||
return x + y
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(add(42, 13))
|
||||
}
|
11
functions.go
Normal file
11
functions.go
Normal file
@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func add(x int, y int) int {
|
||||
return x + y
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(add(42, 13))
|
||||
}
|
7
hello.go
Normal file
7
hello.go
Normal file
@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello, world!")
|
||||
}
|
23
if-and-else.go
Normal file
23
if-and-else.go
Normal file
@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func pow(x, n, lim float64) float64 {
|
||||
if v := math.Pow(x, n); v < lim {
|
||||
return v
|
||||
} else {
|
||||
fmt.Printf("%g >= %g\n", v, lim)
|
||||
}
|
||||
// can't use v here, though
|
||||
return lim
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(
|
||||
pow(3, 2, 10),
|
||||
pow(3, 3, 20),
|
||||
)
|
||||
}
|
20
if-with-a-short-statement.go
Normal file
20
if-with-a-short-statement.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func pow(x, n, lim float64) float64 {
|
||||
if v := math.Pow(x, n); v < lim {
|
||||
return v
|
||||
}
|
||||
return lim
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(
|
||||
pow(3, 2, 10),
|
||||
pow(3, 3, 20),
|
||||
)
|
||||
}
|
17
if.go
Normal file
17
if.go
Normal file
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func sqrt(x float64) string {
|
||||
if x < 0 {
|
||||
return sqrt(-x) + "i"
|
||||
}
|
||||
return fmt.Sprint(math.Sqrt(x))
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(sqrt(2), sqrt(-4))
|
||||
}
|
10
imports.go
Normal file
10
imports.go
Normal file
@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Printf("Now you have %g problems.\n", math.Sqrt(7))
|
||||
}
|
22
making-slices.go
Normal file
22
making-slices.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
a := make([]int, 5)
|
||||
printSlice("a", a)
|
||||
|
||||
b := make([]int, 0, 5)
|
||||
printSlice("b", b)
|
||||
|
||||
c := b[:2]
|
||||
printSlice("c", c)
|
||||
|
||||
d := c[2:5]
|
||||
printSlice("d", d)
|
||||
}
|
||||
|
||||
func printSlice(s string, x []int) {
|
||||
fmt.Printf("%s len=%d cap=%d %v\n",
|
||||
s, len(x), cap(x), x)
|
||||
}
|
16
map-literals-continued.go
Normal file
16
map-literals-continued.go
Normal file
@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Vertex struct {
|
||||
Lat, Long float64
|
||||
}
|
||||
|
||||
var m = map[string]Vertex {
|
||||
"Bell Labs": {40.68433, -74.39967},
|
||||
"Google": {37.42202, -122.08408},
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(m)
|
||||
}
|
20
map-literals.go
Normal file
20
map-literals.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Vertex struct {
|
||||
Lat, Long float64
|
||||
}
|
||||
|
||||
var m = map[string]Vertex {
|
||||
"Bell Labs": Vertex{
|
||||
40.68433, -74.39967,
|
||||
},
|
||||
"Google": Vertex{
|
||||
37.42202, -122.08408,
|
||||
},
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(m)
|
||||
}
|
17
maps.go
Normal file
17
maps.go
Normal file
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Vertex struct {
|
||||
Lat, Long float64
|
||||
}
|
||||
|
||||
var m map[string]Vertex
|
||||
|
||||
func main() {
|
||||
m = make(map[string]Vertex)
|
||||
m["Bell Labs"] = Vertex{
|
||||
40.68433, -74.39967,
|
||||
}
|
||||
fmt.Println(m["Bell Labs"])
|
||||
}
|
12
multiple-results.go
Normal file
12
multiple-results.go
Normal file
@ -0,0 +1,12 @@
|
||||
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)
|
||||
}
|
19
mutating-maps.go
Normal file
19
mutating-maps.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
m := make(map[string]int)
|
||||
|
||||
m["Answer"] = 42
|
||||
fmt.Println("The value:", m["Answer"])
|
||||
|
||||
m["Answer"] = 48
|
||||
fmt.Println("The value:", m["Answer"])
|
||||
|
||||
delete(m, "Answer")
|
||||
fmt.Println("The value:", m["Answer"])
|
||||
|
||||
v, ok := m["Answer"]
|
||||
fmt.Println("The value:", v, "Present?", ok)
|
||||
}
|
13
named-results.go
Normal file
13
named-results.go
Normal file
@ -0,0 +1,13 @@
|
||||
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))
|
||||
}
|
11
nil-slices.go
Normal file
11
nil-slices.go
Normal file
@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var s []int
|
||||
fmt.Println(s, len(s), cap(s))
|
||||
if s == nil {
|
||||
fmt.Println("nil!")
|
||||
}
|
||||
}
|
19
numeric-constants.go
Normal file
19
numeric-constants.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
const (
|
||||
Big = 1 << 100
|
||||
Small = Big >> 99
|
||||
)
|
||||
|
||||
func needInt(x int) int { return x*10 + 1 }
|
||||
func needFloat(x float64) float64 {
|
||||
return x * 0.1
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(needInt(Small))
|
||||
fmt.Println(needFloat(Small))
|
||||
fmt.Println(needFloat(Big))
|
||||
}
|
14
packages.go
Normal file
14
packages.go
Normal file
@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
seed := time.Now().UnixNano()
|
||||
rand.Seed(seed)
|
||||
fmt.Println("Seed: ", seed)
|
||||
fmt.Println("My favorite number is", rand.Intn(10))
|
||||
}
|
16
pointers.go
Normal file
16
pointers.go
Normal file
@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
i, j := 42, 2701
|
||||
|
||||
p := &i
|
||||
fmt.Println(*p)
|
||||
*p = 21
|
||||
p = &j
|
||||
fmt.Println(i)
|
||||
|
||||
*p = *p / 37
|
||||
fmt.Println(j)
|
||||
}
|
13
range-continued.go
Normal file
13
range-continued.go
Normal file
@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
pow := make([]int, 10)
|
||||
for i := range pow {
|
||||
pow[i] = 1 << uint(i) // == 2**i
|
||||
}
|
||||
for _, value := range pow {
|
||||
fmt.Printf("%d\n", value)
|
||||
}
|
||||
}
|
11
range.go
Normal file
11
range.go
Normal file
@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
var pow = []int{1, 2, 3, 4, 8, 16, 32, 64, 128}
|
||||
|
||||
func main() {
|
||||
for i, v := range pow {
|
||||
fmt.Printf("2**%d = %d\n", i, v)
|
||||
}
|
||||
}
|
12
sandbox.go
Normal file
12
sandbox.go
Normal file
@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Welcome to the playground!")
|
||||
|
||||
fmt.Println("The time is", time.Now())
|
||||
}
|
10
short-variable-declaration.go
Normal file
10
short-variable-declaration.go
Normal file
@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var i, j int = 1, 2
|
||||
k := 3
|
||||
c, python, java := true, false, "no!"
|
||||
fmt.Println(i, j, k, c, python, java)
|
||||
}
|
16
slice-bounds.go
Normal file
16
slice-bounds.go
Normal file
@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
s := []int{2, 3, 5, 7, 11, 13}
|
||||
|
||||
s = s[1:4]
|
||||
fmt.Println(s)
|
||||
|
||||
s = s[:2]
|
||||
fmt.Println(s)
|
||||
|
||||
s = s[1:]
|
||||
fmt.Println(s)
|
||||
}
|
24
slice-len-cap.go
Normal file
24
slice-len-cap.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
s := []int{2, 3, 5, 7, 11, 13}
|
||||
printSlice(s)
|
||||
|
||||
// Slice the slive to give it zero length.
|
||||
s = s[:0]
|
||||
printSlice(s)
|
||||
|
||||
// Extend its length
|
||||
s = s[:4]
|
||||
printSlice(s)
|
||||
|
||||
// Drop its first two values
|
||||
s = s[2:]
|
||||
printSlice(s)
|
||||
}
|
||||
|
||||
func printSlice(s []int) {
|
||||
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
|
||||
}
|
24
slice-literals.go
Normal file
24
slice-literals.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
q := []int{2, 3, 5, 7, 11, 13}
|
||||
fmt.Println(q)
|
||||
|
||||
r := []bool{true, false, true, true, false, true}
|
||||
fmt.Println(r)
|
||||
|
||||
s := []struct {
|
||||
i int
|
||||
b bool
|
||||
} {
|
||||
{2, true},
|
||||
{3, false},
|
||||
{5, true},
|
||||
{7, true},
|
||||
{11, false},
|
||||
{13, true},
|
||||
}
|
||||
fmt.Println(s)
|
||||
}
|
26
slices-of-slice.go
Normal file
26
slices-of-slice.go
Normal file
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create a tic-tac-toe board
|
||||
board := [][]string{
|
||||
[]string{"_", "_", "_"},
|
||||
[]string{"_", "_", "_"},
|
||||
[]string{"_", "_", "_"},
|
||||
}
|
||||
|
||||
// The players take turns
|
||||
board[0][0] = "X"
|
||||
board[2][2] = "O"
|
||||
board[1][2] = "X"
|
||||
board[1][0] = "O"
|
||||
board[0][2] = "X"
|
||||
|
||||
for i := 0; i < len(board); i++ {
|
||||
fmt.Printf("%s\n", strings.Join(board[i], " "))
|
||||
}
|
||||
}
|
20
slices-pointers.go
Normal file
20
slices-pointers.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
names := [4]string{
|
||||
"John",
|
||||
"Paul",
|
||||
"George",
|
||||
"Ringo",
|
||||
}
|
||||
fmt.Println(names)
|
||||
|
||||
a := names[0:2]
|
||||
b := names[1:3]
|
||||
|
||||
b[0] = "XXX"
|
||||
fmt.Println(a, b)
|
||||
fmt.Println(names)
|
||||
}
|
10
slices.go
Normal file
10
slices.go
Normal file
@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
primes := [6]int{2, 3, 5, 7, 11, 13}
|
||||
|
||||
var s []int = primes[1:4]
|
||||
fmt.Println(s)
|
||||
}
|
14
struct-fields.go
Normal file
14
struct-fields.go
Normal file
@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Vertex struct {
|
||||
X int
|
||||
Y int
|
||||
}
|
||||
|
||||
func main() {
|
||||
v := Vertex{1, 2}
|
||||
v.X = 4
|
||||
fmt.Println(v.X)
|
||||
}
|
18
struct-literal.go
Normal file
18
struct-literal.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Vertex struct {
|
||||
X, Y int
|
||||
}
|
||||
|
||||
var (
|
||||
v1 = Vertex{1, 2} // has type Vertex
|
||||
v2 = Vertex{X: 1} // Y:0 is implicit
|
||||
v3 = Vertex{} // X:0 and Y:0
|
||||
p = &Vertex{1, 2} // has type *Vertex
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(v1, p, v2, v3)
|
||||
}
|
15
struct-pointers.go
Normal file
15
struct-pointers.go
Normal file
@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Vertex struct {
|
||||
X int
|
||||
Y int
|
||||
}
|
||||
|
||||
func main() {
|
||||
v := Vertex{1, 2}
|
||||
p := &v
|
||||
p.X = 1e9
|
||||
fmt.Println(v)
|
||||
}
|
12
structs.go
Normal file
12
structs.go
Normal file
@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Vertex struct {
|
||||
X int
|
||||
Y int
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(Vertex{1, 2})
|
||||
}
|
21
switch-evaluation-order.go
Normal file
21
switch-evaluation-order.go
Normal file
@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("When's Saturday?")
|
||||
today := time.Now().Weekday()
|
||||
switch time.Saturday {
|
||||
case today + 0:
|
||||
fmt.Println("Today.")
|
||||
case today + 1:
|
||||
fmt.Println("Tomorrow.")
|
||||
case today + 2:
|
||||
fmt.Println("In two days.")
|
||||
default:
|
||||
fmt.Println("Too far away.")
|
||||
}
|
||||
}
|
18
switch-with-no-condition.go
Normal file
18
switch-with-no-condition.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
t := time.Now()
|
||||
switch {
|
||||
case t.Hour() < 12:
|
||||
fmt.Println("Good morning!")
|
||||
case t.Hour() < 17:
|
||||
fmt.Println("Good afternoon.")
|
||||
default:
|
||||
fmt.Println("Good evening.")
|
||||
}
|
||||
}
|
20
switch.go
Normal file
20
switch.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Print("Go runs on ")
|
||||
switch os := runtime.GOOS; os {
|
||||
case "darwin":
|
||||
fmt.Println("OS X.")
|
||||
case "linux":
|
||||
fmt.Println("Linux.")
|
||||
default:
|
||||
// freebsd, openbsd
|
||||
// plan9, windows ...
|
||||
fmt.Print("%s.\n", os)
|
||||
}
|
||||
}
|
13
type-conversions.go
Normal file
13
type-conversions.go
Normal file
@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var x, y int = 3, 4
|
||||
var f float64 = math.Sqrt(float64(x*x + y*y))
|
||||
var z uint = uint(f)
|
||||
fmt.Println(x, y, z)
|
||||
}
|
8
type-inference.go
Normal file
8
type-inference.go
Normal file
@ -0,0 +1,8 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
v := 42
|
||||
fmt.Printf("v is of type %T\n", v)
|
||||
}
|
10
variables-with-initializers.go
Normal file
10
variables-with-initializers.go
Normal file
@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
var i, j int = 1, 2
|
||||
|
||||
func main() {
|
||||
var c, python, java = true, false, "no!"
|
||||
fmt.Println(i, j, c, python, java)
|
||||
}
|
10
variables.go
Normal file
10
variables.go
Normal file
@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
var c, python, java bool
|
||||
|
||||
func main() {
|
||||
var i int
|
||||
fmt.Println(i, c, python, java)
|
||||
}
|
Loading…
Reference in New Issue
Block a user