溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

golang基礎(chǔ)練習(xí)(一)

發(fā)布時(shí)間:2020-06-29 10:56:49 來源:網(wǎng)絡(luò) 閱讀:656 作者:PowerMichael 欄目:編程語言
//遍歷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)
    }

    }

//匿名函數(shù)
package main

import "fmt"

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

    fmt.Println(f(2,3))

    }

//defer  延遲執(zhí)行,后進(jìn)先出
//用法:執(zhí)行關(guān)閉
package main

import "fmt"

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

//defer 延遲執(zhí)行

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

}

//函數(shù)作為一種數(shù)據(jù)類型
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
    // 單獨(dú)使用return,返回所有返回值
    return
}

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

//Variables
package main

import "fmt"

//一行聲明多個(gè)變量,并且也可以賦值多個(gè)變量
//整型默認(rèn)值0,bool類型默認(rèn)false
//
var c, python, java bool
var one, two, three int
var s1, s2, s3 string
//下面演示一個(gè)錯(cuò)誤的示范,函數(shù)主機(jī)外不能賦值
//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"
)

//聲明相同的數(shù)據(jù)類型,可以用()一起申明
var (
    ToBe   bool       = false
    MaxInt uint64     = 1<<64 - 1
    z      complex128 = cmplx.Sqrt(-5 + 12i)
)

//printf函數(shù)中%T輸出數(shù)據(jù)類型
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
//編譯器自行推理數(shù)據(jù)類型
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.
//需要將循環(huán)終止條件放在代碼語句中
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"
)

//傳入的參數(shù)通過math函數(shù)功能得出結(jié)果后同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
//延遲執(zhí)行

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.
//后進(jìn)先出規(guī)則

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 通過結(jié)構(gòu)體指針可以訪問結(jié)構(gòu)體字段,注意被訪問的變量要大寫

//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]以閉區(qū)間開始,開區(qū)間結(jié)束
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個(gè)元素到第1個(gè)元素結(jié)束
    s = s[:2]
    fmt.Println(s)

    //從第一個(gè)元素開始,最后一個(gè)元素結(jié)束
    s = s[1:]
    fmt.Println(s)
}

//Slice length and capacity
//slice引用的是數(shù)組中的內(nèi)存地址,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.
//切片的容量是潛在數(shù)組的元素個(gè)數(shù),從切片的第一個(gè)元素開始算起
//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.
//其實(shí)就是可動(dòng)態(tài)變化的多維數(shù)組

//計(jì)算機(jī)的思維去理解程序

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() {
    //定義一個(gè)空切片,長度和容量都為0
    var s []int
    printSlice(s)

    // append works on nil slices.
    //在切片中追加一個(gè)元素,默認(rèn)長度為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函數(shù)聲明一個(gè)整型切片,變量名為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
}

//聲明一個(gè)map類型變量m
var m map[string]Vertex

//聲明一個(gè)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.
//如果頂級(jí)類型僅僅是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"])

    //空字符默認(rèn)為" "
    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"])

    //空數(shù)字為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.
//記?。阂粋€(gè)方法就是帶著接收者參數(shù)的函數(shù)。
//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)
}
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI