美文网首页go
go 基本知识回顾

go 基本知识回顾

作者: 达摩君 | 来源:发表于2018-06-13 11:43 被阅读5次

    1.变量定义

    • var x int
      y := 100 -->此方法不能定义在函数体外
    • const 常量
    • array
      var x [10]int
      y := [10]int{1,2,3,4,5}
    • slice -动态数组
      var x []int
      y := make([]int, len, cap)
    • map
      student :=make( map[string]int)

    make用于内建类型(slice,map,channel)的内存分配

    2.流程控制

    • if
    if x > 10 {
    } else if x > 100 {
    } else {
    }
    
    • for
    for x := 1 ; x <= 100; x ++ {
    }
    
    for x <= 100 {
    }
    
    for {
    } //死循环
    
    • switch
    switch x {
       case 1: 
        ....
    //break  省略
       case 2:
        ....
         fallthrough  //穿透下去
       case 3:
        ....
       default:
        .....
    }
    
    • range
    x := [5]int{1,2,3,4,5}
    for i,v := range x {
      //I:key v: value
    }
    for _,v := range x {
      // v: value
    }
    

    函数

    func haha(x int, y int) ( int, int) {
    .....
      return a,b
    }
    //x,y 都是int
    func haha(x , y int) ( int, int) {
    .....
      return a,b
    }
    //匿名函数
    f := func(x, y int) int {
      return x+y
    }
    f(2,3)
    
    
    • defer
    for i := 1 ; i <5 ; i++ {
      defer fmt.PrintIn(i)
    }
    fmt.PrintIn("hehe")
    /*
    hehe
    4
    3
    2
    1
    */
    
    • panic
    //抛出异常
     panic("wrong~")
    

    结构体

    • struct
    type Haha struct {
          name string
          age int
    }
    type Hehe struct {
        Haha  //包含Haha所有字段
        price int
    }
    

    面向对象

    • 一个类只需要实现了接口要求的所有函数,我们就说这个类实现了该接口。
    • 类型判断
    if v, ok := v1.(float); ok {
        //是float类型 
    }
    

    协程

    • 轻量级
    1. 该任务的业务代码主动要求切换,即主动让出执行权
      runtime.Gosched();
    2. 发生了IO,导致执行阻塞。
    • go + 函数名: 启动一个协程
    • channel goroutine间的通信方式
    ch := make(chan int)
    a := <-ch  //读
    ch <- b  //写
    
    //缓冲channel
    c := make(chan int, n)
    
    • select
      用于处理异步i/o问题
    select {
      case <- chan1:
      case chan2 <-1:
      default:
    }
    

    JSON

    第三方:ffjson

    //对数组类型的json编码
    x := [5]int{1,2,3,4,5}
    s, err := json.Marshal(x)
    if err != nil {
      panic(err)
    }
    fmt.PrintIn(string(s))
    //对map类型的json编码
    m := make(map[string]float64)
    m["zhangs"] = 60.4
    s2, err := json.Marshal(m)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(s2))
    
    type Student struct {
        Name string `json:"student_name"`
        Age int  //必须要大写
    }
    func main() {
    //对象json
        student := Student{"li",18}
        ss, err := json.Marshal(student)
        if err != nil {
            panic(err)
        }
        fmt.Println(string(ss))
            
            //对ss进行json解码
        var sss interface{}
        json.Unmarshal(ss, &sss)
        fmt.Printf("%v",sss)
    }
    

    MD5

    import (
        "crypto/md5"
        "fmt"
    )
    
    func main() {
        md5inset := md5.New()
        md5inset.Write([]byte("lee"))
        Result := md5inset.Sum([]byte(""))
        fmt.Printf("%x\n",Result)
    }
    

    HTTP

    import "net/http"
    
    func main() {
        http.HandleFunc("/hello", func(writer http.ResponseWriter, request *http.Request) {
            writer.Write([]byte("hello, lee"))
        })
        http.ListenAndServe("127.0.0.1:8080",nil)
    }
    

    正则表达式

    func main() {
        isok, _ := regexp.Match("[a-zA-Z]",[]byte("lee"))
        fmt.Println(isok)
        isok1, _ := regexp.MatchString("[a-zA-Z]","lee")
        fmt.Println(isok1)
        reg := regexp.MustCompile("lee")
        result := reg.FindAllString("leejianglee",-1)
        fmt.Println(result)
    
        reg1 := regexp.MustCompile(`^l(.*)`)
        result1 := reg1.FindAllString("leejianglee",-1)
        fmt.Println(result1)
    
        reg2 := regexp.MustCompile(`^l(.{1})(.*)o$`)
        //捕获
        result2 := reg2.FindAllStringSubmatch("leejiangbo",-1)
        fmt.Println(result2)
    }
    

    Mysql

    相关文章

      网友评论

        本文标题:go 基本知识回顾

        本文链接:https://www.haomeiwen.com/subject/dzsasftx.html