golang基础练习(一)

//遍历map
package main

import "fmt"

func main()  {
    x := make(map[string]int)

    x["zhangsan"] = 3
    x["lisi"] = 4
    x["wangwu"] = 5

    //#丢弃值
    for i,_ := range x {
        fmt.Println(i)
    }

    }

//匿名函数
package main

import "fmt"

func main()  {
    f := func(x, y int) int{
        return x + y
    }

    fmt.Println(f(2,3))

    }

//defer  延迟执行,后进先出
//用法:执行关闭
package main

import "fmt"

func main()  {
    for i := 1; i <= 5; i++{
        defer fmt.Println(i)
    }
    fmt.Println("before defer")
    }

//defer 延迟执行

package main

import "fmt"

func main()  {
    defer func() {
        fmt.Print("after defer")
    }()
    fmt.Println("before defer")
    }

//异常

package main

import "fmt"

func main()  {
    defer func() {
        fmt.Print("after defer")
    }()
    panic("I am wrong~!")
    fmt.Println("before defer")

}

//函数作为一种数据类型
package main

import "fmt"

func main()  {
    type sum func(x, y int) int
    var f sum = func(x, y int) int {
        return x + y
    }
    fmt.Println(f(3, 4))

}

//struct,struct有继承的特性
package main

import "fmt"

type Person struct {
    name string
    age int
}

type Student struct {
    Person
    speciality string
}

func main()  {
    student := Student{Person{"zhangsan", 23}, "maths"}
    person := Person{"zhangsan",23}
    fmt.Printf("%v", person.name)
    fmt.Printf("%v", student.speciality)

}

//return 
package main

import "fmt"

func split(sum int) (x, y int) {
    x = sum * 4 / 9
    y = sum - x
    // 单独使用return,返回所有返回值
    return
}

func main() {
    fmt.Println(split(17))
}

//Variables
package main

import "fmt"

//一行声明多个变量,并且也可以赋值多个变量
//整型默认值0,bool类型默认false
//
var c, python, java bool
var one, two, three int
var s1, s2, s3 string
//下面演示一个错误的示范,函数主机外不能赋值
//s1, s2, s3 = "string1", "string2", "string3"
func main() {
    var i int
    fmt.Println(i, c, python, java)
    fmt.Println(i, one, two, three)
    s1, s2, s3 = "string1", "string2", "string3"
    fmt.Println(s1, s2, s3)
}

//Variables with initializers
package main

import "fmt"

var i, j int = 1, 2

func main() {
    //golang直接声明赋值一步到位
    var c, python, java = true, false, "no!"
    fmt.Println(i, j, c, python, java)
}

//Short variable declarations

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)
}

//Basic types

package main

import (
    "fmt"
    "math/cmplx"
)

//声明相同的数据类型,可以用()一起申明
var (
    ToBe   bool       = false
    MaxInt uint64     = 1<<64 - 1
    z      complex128 = cmplx.Sqrt(-5 + 12i)
)

//printf函数中%T输出数据类型
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)
}

//Zero 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)
}

//Type conversions

package main

import (
    "fmt"
    "math"
)

func main() {
    var x, y int = 3, 4
    var v float64 = 5.0111
    fmt.Println(v)
    var f float64 = math.Sqrt(float64(x*x + y*y))
    b  := int(v)
    var z uint = uint(f)
    fmt.Println(x, y, z,b)
}

//Type inference
//编译器自行推理数据类型
package main

import "fmt"

func main() {
    v := 42 // change me!
    s := "hello world"
    fmt.Printf("v is of type %T %T\n", v, s)
}

//Constants
//Constants cannot be declared using the := syntax.
package main

import "fmt"

const Pi = 3.14

func main() {
    const World = "世界"
    fmt.Println("Hello", World)
    fmt.Println("Happy", Pi, "Day")

    const Truth = true
    fmt.Println("Go rules?", Truth)
}

//Numeric Constants

package main

import "fmt"

const (
    // Create a huge number by shifting a 1 bit left 100 places.
    // In other words, the binary number that is 1 followed by 100 zeroes.
    Big = 1 << 100
    // Shift it right again 99 places, so we end up with 1<<1, or 2.
    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))
}

//for 
package main

import "fmt"

func main() {
    sum := 0
    for i := 0; i < 100; i++ {
        fmt.Println(i)
        sum += i
    }
    fmt.Println("sum:", sum)
}

//For continued

//The init and post statements are optional.

package main

import "fmt"

func main() {
    sum := 1
    for ; sum < 1000; {
        sum += sum
        fmt.Println(sum)
    }
    fmt.Println(sum)
}

//For is Go's "while"
//At that point you can drop the semicolons: C's while is spelled for in Go.
//需要将循环终止条件放在代码语句中
package main

import "fmt"

func main() {
    sum := 1
    for sum < 1000 {
        sum += sum
    }
    fmt.Println(sum)
}

//Forever

// /If you omit the loop condition it loops forever, so an infinite loop is compactly expressed.

package main

func main() {
    for {
    }
}

//If

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))
}

//If with a short statement

package main

import (
    "fmt"
    "math"
)

//传入的参数通过math函数功能得出结果后同lim做条件判断
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),
    )
}

//Switch evaluation order

package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println("When's Wednesday?")
    today := time.Now().Weekday()
    fmt.Println("Today:", today)
    switch time.Wednesday {
    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.")
    }
}

//Switch with no condition

//Switch without a condition is the same as switch true.This construct can be a clean way to write long if-then-else chains.
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.")
    }
}

// Defer
//延迟执行

package main

import "fmt"

func main() {
    defer fmt.Println("world")

    fmt.Println("hello")
    fmt.Println("world")
    fmt.Println("say once again")
    fmt.Println("hello")
}

//Stacking defers

//Deferred function calls are pushed onto a stack. When a function returns, its deferred calls are executed in last-in-first-out order.
//后进先出规则

package main

import "fmt"

func main() {
    fmt.Println("counting")

    for i := 0; i < 10; i++ {
        defer fmt.Println(i)
    }

    fmt.Println("done")
}

//Pointers

//The type *T is a pointer to a T value. Its zero value is nil.
//The & operator generates a pointer to its operand.

package main

import "fmt"

func main() {
    i, j := 42, 2701

    p := &i         // point to i
    fmt.Println(&i)
    fmt.Println(&p)
    fmt.Println(*p) // read i through the pointer
    *p = 21         // set i through the pointer
    fmt.Println(i)  // see the new value of i

    p = &j         // point to j
    *p = *p / 37   // divide j through the pointer
    fmt.Println(j) // see the new value of j
    fmt.Println(&j)
}

//Structs. A struct is a collection of fields.

package main

import "fmt"

type Vertex struct {
    X int
    Y int
}

func main() {
    fmt.Println(Vertex{1, 2})
}

//Struct Fields. Struct fields are accessed using a dot.

package main

import "fmt"

type Vertex struct {
    X int
    Y int
}

func main() {
    v := Vertex{1, 2}
    v.X = 4
    fmt.Println(v.X)
}

//Pointers to structs 通过结构体指针可以访问结构体字段,注意被访问的变量要大写

//Struct fields can be accessed through a struct pointer.

package main

import "fmt"

type Vertex struct {
    X int
    Y int
}

func main() {
    v := Vertex{1, 2}
    p := &v
    p.X = 100
    fmt.Println(v)
}

//Struct Literals

package main

import "fmt"

type Vertex struct {
    X, Y int
}

var (
    v1 = Vertex{1, 2}  // has type Vertex
    v2 = Vertex{X: 2}  // Y:0 is implicit
    v3 = Vertex{}      // X:0 and Y:0
    v4 = Vertex{Y:5}
    p  = &Vertex{1, 2} // has type *Vertex
)

func main() {
    fmt.Println(v1, p, v2, v3, v4)
}

// Arrays

//The type [n]T is an array of n values of type T.
/*
The expression

var a [10]int

*/

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)

    bb :=[2]bool{false, true}
    fmt.Println(bb)
}

//Slices
//An array has a fixed size. A slice, on the other hand, is a dynamically-sized, flexible view into the elements of an array. In practice, slices are much more common than arrays.
//The type []T is a slice with elements of type T.

package main

import "fmt"
//[1:4]以闭区间开始,开区间结束
func main() {
    primes := [6]int{2, 3, 5, 7, 11, 13}

    var s []int = primes[1:4]
    fmt.Println(s)
}

//Slices are like references to arrays
/*
A slice does not store any data, it just describes a section of an underlying array.

Changing the elements of a slice modifies the corresponding elements of its underlying array.

Other slices that share the same underlying array will see those changes.
*/

package main

import "fmt"

func main() {
    names := [4]string{
        "John",
        "Paul",
        "George",
        "Ringo",
    }
    fmt.Println(names)

    a := names[0:2]
    b := names[1:3]
    fmt.Println(a, b)

    b[0] = "XXX"
    fmt.Println(a, b)
    fmt.Println(names)
}

// Slice literals

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)

    f := []float64{1.11111, 2.22222, 3.33333, 4.44444}
    fmt.Println(f)

    s := []struct {
        i int
        b bool
    }{
        {2, true},
        {3, false},
        {5, true},
        {7, true},
        {11, false},
        {13, true},
    }
    fmt.Println(s)
}

//Slice defaults
/*
When slicing, you may omit the high or low bounds to use their defaults instead.

The default is zero for the low bound and the length of the slice for the high bound.

For the array

var a [10]int
these slice expressions are equivalent:

a[0:10]
a[:10]
a[0:]
a[:]

*/

package main

import "fmt"

func main() {
    s := []int{2, 3, 5, 7, 11, 13}

    s = s[1:4]
    fmt.Println(s)

    //从第0个元素到第1个元素结束
    s = s[:2]
    fmt.Println(s)

    //从第一个元素开始,最后一个元素结束
    s = s[1:]
    fmt.Println(s)
}

//Slice length and capacity
//slice引用的是数组中的内存地址,array在使用过程中是指传递
//A slice has both a length and a capacity.
//The length of a slice is the number of elements it contains.
// The capacity of a slice is the number of elements in the underlying array, counting from the first element in the slice.
//切片的容量是潜在数组的元素个数,从切片的第一个元素开始算起
//The length and capacity of a slice s can be obtained using the expressions len(s) and cap(s).

//You can extend a slice's length by re-slicing it, provided it has sufficient capacity. Try changing one of the slice operations in the example program to extend it beyond its capacity and see what happens.

package main

import "fmt"

func main() {
    s := []int{2, 3, 5, 7, 11, 13}
    printSlice(s)

    // Slice the slice to give it zero length.
    s = s[:1]
    printSlice(s)

    // Extend its length.
    s = s[:4]
    printSlice(s)

    // Drop its first two values.
    s = s[3:]
    printSlice(s)
}

func printSlice(s []int) {
    fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}

// Nil slices

/*
The zero value of a slice is nil.

A nil slice has a length and capacity of 0 and has no underlying array.
空切片的长度和容量为0,值为nil
*/

package main

import "fmt"

func main() {
    var s []int
    fmt.Println(s, len(s), cap(s))
    if s == nil {
        fmt.Println("nil!")
    }
}

//Creating a slice with make
/*
Slices can be created with the built-in make function; this is how you create dynamically-sized arrays.
The make function allocates a zeroed array and returns a slice that refers to that array:

a := make([]int, 5)  // len(a)=5
To specify a capacity, pass a third argument to make:

b := make([]int, 0, 5) // len(b)=0, cap(b)=5

b = b[:cap(b)] // len(b)=5, cap(b)=5
b = b[1:]      // len(b)=4, cap(b)=4

*/

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)
}

//Slices of slices

//Slices can contain any type, including other slices.
//其实就是可动态变化的多维数组

//计算机的思维去理解程序

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[0][1] = "X"
    board[1][2] = "X"
    board[1][0] = "O"
    board[1][1] = "0"
    board[1][2] = "0"
    board[0][2] = "X"

    for i := 0; i < len(board); i++ {
        fmt.Printf("%s\n", strings.Join(board[i], " "))
    }
}

//Appending to a slice

package main

import "fmt"

func main() {
    //定义一个空切片,长度和容量都为0
    var s []int
    printSlice(s)

    // append works on nil slices.
    //在切片中追加一个元素,默认长度为1,容量为2
    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)
    s = append(s, 5, 6, 7, 9)
    printSlice(s)
}

func printSlice(s []int) {
    fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}

//Range

/*
The range form of the for loop iterates over a slice or map.

When ranging over a slice, two values are returned for each iteration. The first is the index, and the second is a copy of the element at that index.

*/

package main

import "fmt"

var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}

func main() {
    for i, v := range pow {
        fmt.Printf("2**%d = %d\n", i, v)
    }
}

//simple add method
package main

import "fmt"

var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
var sum int = 0
func main() {
    for _, v := range pow {
        sum += v    
    }
    fmt.Println(sum)
}

//Range continued

//You can skip the index or value by assigning to _.
//If you only want the index, drop the , value entirely.
//你可以跳过索引的遍历,或者只是想要遍历索引,那么把后面的, value去掉

package main

import "fmt"

func main() {
    //使用make函数声明一个整型切片,变量名为pow
    pow := make([]int, 32)
    for i := range pow {
        pow[i] = 1 << uint(i) // == 2**i
        fmt.Printf("%d\n", i)
    }
    for _, value := range pow {
        fmt.Printf("%d\n", value)
    }
}

//Exercise: Slices
//how to use slice in code?

// Maps

package main

import "fmt"

type Vertex struct {
    Lat, Long float64
}

type NbaPlayers struct {
    lastname, firstname string
}

//声明一个map类型变量m
var m map[string]Vertex

//声明一个map类型变量n
var n map[string]NbaPlayers

var b map[string]int

func main() {

    m = make(map[string]Vertex)
    n = make(map[string]NbaPlayers)
    b = make(map[string]int)
    m["Bell Labs"] = Vertex{
        40.68433, -74.39967,
    }
    n["nba superstart"] = NbaPlayers{"Lebron", "James"}
    b["string"] = 1
    fmt.Println(m["Bell Labs"])
    fmt.Println(n["nba superstart"])
    fmt.Println(b["string"])
}

//Map literals

//Map literals are like struct literals, but the keys are required.

package main

import "fmt"

type Vertex struct {
    Lat, Long float64
}

type Person struct {
    height int
    hair string
}

var m = map[string]Vertex{
    "Bell Labs": Vertex{
        40.68433, -74.39967,
    },
    "Google": Vertex{
        37.42202, -122.08408,
    },
}

var n = map[string]Person{
    "michael":Person{
        172, "black",
    },
    "coco":Person{
        165, "brown",
    },
}

func main() {
    fmt.Println(m)
    fmt.Println(n)
}

//Map literals continued

//If the top-level type is just a type name, you can omit it from the elements of the literal.
//如果顶级类型仅仅是type类型,你可以从文本元素中省略type类型。

package main

import "fmt"

type Vertex struct {
    Lat, Long float64
}

type Person struct {
    height int
    hair string
}

var m = map[string]Vertex{
    "Bell Labs": {40.68433, -74.39967},
    "Google":    {37.42202, -122.08408},
}

var n = map[string]Person{
    "michael":{
        172, "black",
    },
    "coco":{
        165, "brown",
    },
}

func main() {
    fmt.Println(m)
    fmt.Println(n)
}

//Mutating Maps 改变map类型
//Insert or update an element in map m:

package main

import "fmt"

func main() {

    n := make(map[string]string)

    n["Players"] = "Kobe"
    n["Players"] = "James"
    fmt.Println("The value", n["Players"])

    //空字符默认为" "
    delete(n, "Players")
    fmt.Println("The value:", n["Players"])

    m := make(map[string]int)

    m["Answer"] = 42
    fmt.Println("The value:", m["Answer"])

    m["Answer"] = 48
    fmt.Println("The value:", m["Answer"])

    //空数字为0
    delete(m, "Answer")
    fmt.Println("The value:", m["Answer"])

    m["Answer"] = 60

    //如果key“Answer”在m map中,那么ok就是true
    v, ok := m["Answer"]
    fmt.Println("The value:", v, "Present?", ok)
}

//Exercise: Maps
//how to use maps in code?

//Function values
//Functions are values too. They can be passed around just like other values.

//Function values may be used as function arguments and return values.

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))
}

//Function closures
//

//Methods
package main

import (
    "fmt"
    "math"
)

type Vertex struct {
    X, Y float64
}

func (v Vertex) Abs() float64 {
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func main() {
    v := Vertex{3, 4}
    fmt.Println(v.Abs())
}

//Methods are functions
//Remember: a method is just a function with a receiver argument.
//记住:一个方法就是带着接收者参数的函数。
//Here's Abs written as a regular function with no change in functionality.

package main

import (
    "fmt"
    "math"
)

type Vertex struct {
    X, Y float64
}

func Abs(v Vertex) float64 {
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func main() {
    v := Vertex{3, 4}
    fmt.Println(Abs(v))
}

//Methods continued
//You can declare a method on non-struct types, too.

package main

import (
    "fmt"
    "math"
)

type MyFloat float64

func (f MyFloat) Abs() float64 {
    if f < 0 {
        return float64(-f)
    }
    return float64(f)
}

func main() {
    f := MyFloat(-math.Sqrt2)
    fmt.Println(f.Abs())
}

//Pointer receivers

package main

import (
    "fmt"
    "math"
)

type Vertex struct {
    X, Y float64
}

func (v Vertex) Abs() float64 {
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func (v *Vertex) Scale(f float64) {
    v.X = v.X * f
    v.Y = v.Y * f
}

func main() {
    v := Vertex{3, 4}
    v.Scale(10)
    fmt.Println(v.Abs())
}

//Pointers and functions

package main

import (
    "fmt"
    "math"
)

type Vertex struct {
    X, Y float64
}

func Abs(v Vertex) float64 {
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func Scale(v *Vertex, f float64) {
    v.X = v.X * f
    v.Y = v.Y * f
}

func main() {
    v := Vertex{3, 4}
    Scale(&v, 10)
    fmt.Println(Abs(v))
}

//Methods and pointer indirection

package main

import "fmt"

type Vertex struct {
    X, Y float64
}

func (v *Vertex) Scale(f float64) {
    v.X = v.X * f
    v.Y = v.Y * f
}

func ScaleFunc(v *Vertex, f float64) {
    v.X = v.X * f
    v.Y = v.Y * f
}

func main() {
    v := Vertex{3, 4}
    v.Scale(2)
    ScaleFunc(&v, 10)

    p := &Vertex{4, 3}
    p.Scale(3)
    ScaleFunc(p, 8)

    fmt.Println(v, p)
}

猜你喜欢

转载自blog.51cto.com/huwho/2312581