zoukankan      html  css  js  c++  java
  • Go基础---->go的基础学习(一)

      这里面记录一些学习go的基础知识。我希望爱我的人不寂寞,我希望我爱的人喜欢我

    go的基础知识

     一、go中的map的使用

    package main 
    
    import (
        "fmt"
    )
    
    type PersonInfo struct {
        ID string
        Name string
        Address string
    }
    
    func main() {
        // declare map
        // var personDB map[string] PersonInfo
    
        // create map
        var personDB = make(map[string] PersonInfo, 100)
    
        // insert data
        personDB["LL"] = PersonInfo{"123456", "LL", "wuhan"}
        personDB["huhx"] = PersonInfo{"123456", "huhx", "huanggang"}
    
        // query key is exiest
        person, ok := personDB["huhx"]
        if ok {
            fmt.Println("Found Person: ", person.Name, ", address", person.Address) // Found Person:  huhx , address huanggang
        } else {
            fmt.Println("No person found")
        }
    
        // delete key from map
        fmt.Println("Before delete: ", len(personDB)) // Before delete:  2
        delete(personDB, "huhx")
        fmt.Println("After delete: ", len(personDB)) // After delete:  1
    }

    二、go中数组切片的使用

    package main 
    
    import "fmt"
    
    func printArray(array []int) {
        for _, v := range array {
          fmt.Print(v, " ") 
      }
      fmt.Println()
    }
    
    func main() {
        mySlice := make([]int, 5, 10)
        fmt.Println("len: ", len(mySlice)) // len:  5
        fmt.Println("cap: ", cap(mySlice)) // cap:  10
    
        // append elements
        mySlice = append(mySlice, 1, 2, 3)
        mySlice2 := []int{8, 9, 10}
        mySlice = append(mySlice, mySlice2...) 
    
        // get data
        printArray(mySlice) // 0 0 0 0 0 1 2 3 8 9 10
    
        // slice from array
        newSlice := mySlice[3:]
        printArray(newSlice) // 0 0 1 2 3 8 9 10
    
       // content copy
      slice1 := []int{1, 2, 3, 4, 5}
        slice2 := []int{5, 4, 3}
        copy(slice2, slice1) // 只会复制slice1的前3个元素到slice2中
        printArray(slice2) // 1 2 3
        slice2 = []int{5, 7, 8, 9}
        copy(slice1, slice2) // 只会复制slice2的4个元素到slice1的前4个位置
        printArray(slice1) // 5 7 8 9 5
    }

    三、go中的数组是一个值类型

    在Go语言中数组是一个值类型(value type)。所有的值类型变量在赋值和作为参数传递时都将产生一次复制动作。如果将数组作为函数的参数类型,则在函数调用时该参数将发生数据复制。

    package main 
    
    import "fmt"
    
    func modify(array [5]int) {
        array[0] = 10
        fmt.Println("In modify(), array values: ", array) // In modify(), array values:  [10 2 3 4 5]
    }
    
    func main() {
        array := [5]int{1, 2, 3, 4, 5}
        modify(array)
        fmt.Println("In main(), array values: ", array) // In main(), array values:  [1 2 3 4 5]
    }

    四、go中的关于循环流程

    package main 
    
    import "fmt"
    
    func exampleIf(x int) string {
        var result string
        if x == 0 {
            result = "huhx"
        } else {
            result = "LL"
        }
        return result
    }
    
    func exampleSwitch(i int) {
        switch i {
            case 0:
                fmt.Print("huhx 0 ")
                fallthrough
            case 1:
                fmt.Print("linux 0 ")
            case 2:
                fmt.Print("LL 0 ")
            default:
                fmt.Println("tomhu 0 ")
        } 
    }
    
    func exampleFor(x int) int {
        sum := 0
        for i := 0; i < x; i++ {
            sum += i
        }
        return sum
    }
    
    func exampleWhile(x int) int{
        sum := 0
        for {
            sum ++
            if sum > x {
                break
            }
        }
        return sum
    }
    
    func exampleGoto(x int) {
        i := 0
        HERE:
        fmt.Print(i, " ")
        i ++
        if i < x {
            goto HERE
        }
    }
    
    func main() {
        // test if
        fmt.Println(exampleIf(0)) // huhx
    
        // test switch
        exampleSwitch(0) // huhx 0 linux 0
        fmt.Println()
    
        // test for
        fmt.Println(exampleFor(10)) // 45
    
        // test while
        fmt.Println(exampleWhile(100)) // 101
    
        exampleGoto(10) // 0 1 2 3 4 5 6 7 8 9
    }

    友情链接

  • 相关阅读:
    微信小程序自定义navigationBar
    微信小程序-自动定位并将经纬度解析为具体地址
    a conexant audio couldnot befound
    平衡二叉树(AVL)java实现
    二叉树的各种非递归遍历
    选择排序
    快速排序
    冒泡排序
    数组洗牌
    haffman树c实现
  • 原文地址:https://www.cnblogs.com/huhx/p/baseusego1.html
Copyright © 2011-2022 走看看