Go问题

作者: echo0x | 来源:发表于2020-06-01 16:54 被阅读0次

    1.导出csv

    go导出CSV中字符串带有英文逗号,只需要在字符串两端加上"\""
    https://www.cnblogs.com/softidea/p/5313516.html
    

    2.go 转json的问题

    1、slice 的话需要&
    var getOrderSlice []*TempOrder
    err = json.Unmarshal([]byte(orderData), &getOrderSlice)
    2、struct 的话不需要&
    coun := &Guider{}
    err = json.Unmarshal([]byte(nc), coun)
    

    3.引入包需要引入的是文件夹名称而不是包名

    rrdEsExport "git.twxrrd.com/wh.tech.rrd/daemon.jobs/model/wxrrd.ex.export"
    不是
    rrdEsExport "git.twxrrd.com/wh.tech.rrd/daemon.jobs/model/rrd_es_export"
    package rrd_es_export
    

    4.字符串拼接方法

    1、+号拼接
    func StringPlus() string{
        var s string
        s+="昵称"+":"+"飞雪无情"+"\n"
        s+="博客"+":"+"http://www.flysnow.org/"+"\n"
        s+="微信公众号"+":"+"flysnow_org"
        return s
    }
    2、fmt 拼接
    func StringFmt() string{
        return fmt.Sprint("昵称",":","飞雪无情","\n","博客",":","http://www.flysnow.org/","\n","微信公众号",":","flysnow_org")
    }
    3、Join 拼接
    func StringJoin() string{
        s:=[]string{"昵称",":","飞雪无情","\n","博客",":","http://www.flysnow.org/","\n","微信公众号",":","flysnow_org"}
        return strings.Join(s,"")
    }
    4、buffer 拼接
    func StringBuffer() string {
        var b bytes.Buffer
        b.WriteString("昵称")
        b.WriteString(":")
        b.WriteString("飞雪无情")
        b.WriteString("\n")
        b.WriteString("博客")
        b.WriteString(":")
        b.WriteString("http://www.flysnow.org/")
        b.WriteString("\n")
        b.WriteString("微信公众号")
        b.WriteString(":")
        b.WriteString("flysnow_org")
        return b.String()
    }
    5、builder 拼接
    func StringBuilder() string {
        var b strings.Builder
        b.WriteString("昵称")
        b.WriteString(":")
        b.WriteString("飞雪无情")
        b.WriteString("\n")
        b.WriteString("博客")
        b.WriteString(":")
        b.WriteString("http://www.flysnow.org/")
        b.WriteString("\n")
        b.WriteString("微信公众号")
        b.WriteString(":")
        b.WriteString("flysnow_org")
        return b.String()
    }
    
    str:="chinese"
        city:="beijing"
        
        // 1. +=
        s:=time.Now()
        for i:=0;i<100000;i++ {
            str +=city
        }
        e:=time.Since(s)
        fmt.Println("time cost 1:", e)
      
        // 2. fmt.Sprintf
        str="chinese"
        city="beijing"
        s=time.Now()
        for i:=0;i<100000;i++ {
            str = fmt.Sprintf("%s%s",str,city)
        }
        e=time.Since(s)
        fmt.Println("time cost 2:", e)
      
         //3.  buffer.WriteString
        str="chinese"
        city="beijing"
        s=time.Now()
        var buf= bytes.Buffer{}
        buf.WriteString(str)
        for i:=0;i<100000;i++ {
            buf.WriteString(city)
        }
        e=time.Since(s)
        fmt.Println("time cost 3:", e)
    
       //4. append
        str="chinese"
        city="beijing"
        s=time.Now()
        bstr:=[]byte(str)
        bcity:=[]byte(city)
        for i:=0;i<100000;i++ {
            bstr= append(bstr, bcity...)
        }
        e=time.Since(s)
        fmt.Println("time cost 4:", e)
    
      // 5. copy
        str="chinese"
        city="beijing"
        s=time.Now()
        zstr :=[]byte(str)
        for i:=0;i<100000;i++ {
            copy(zstr, city)
        }
        e=time.Since(s)
        fmt.Println("time cost 5:", e)
        
        time cost 1: 3.176250251s
        time cost 2: 5.347827717s
        time cost 3: 1.051104ms
        time cost 4: 769.258µs
        time cost 5: 323.968µs
    

    5. gin框架PostForm

    1. 获取 Content-Type:application/x-www-form-urlencoded

    接收参数

    orderNo := c.PostForm("order_no")
    

    post请求

    ## request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    
    func CurlPostStringData(stringData string, url string) ([]byte, error) {
        //body := strings.NewReader("order_no=cjb&name=xiao")
        body := strings.NewReader(stringData)
        request, err := http.NewRequest("POST", url, body)
        if err != nil {
            log.WithChannel("curl post").Printf("url=%s,data=%s,http NewRequest error is %v", url, stringData, err)
            return nil, err
        }
        request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
        //使用连接池httpClient
        httpClient := &http.Client{}
        httpClient.Transport = &http.Transport{
            Proxy: http.ProxyFromEnvironment,
            DialContext: (&net.Dialer{
                Timeout:   30 * time.Second,
                KeepAlive: 30 * time.Second,
                DualStack: true,
            }).DialContext,
            MaxIdleConns:          100,
            MaxIdleConnsPerHost:   100,
            IdleConnTimeout:       90 * time.Second,
            TLSHandshakeTimeout:   90 * time.Second,
            ExpectContinueTimeout: 1 * time.Second,
        }
    
        resp, err := httpClient.Do(request)
        if err != nil {
            log.WithChannel("curl post").Printf("url=%s,data=%s,client Do request error is %v", url, string(stringData), err)
            return nil, err
        }
        respBytes, err := ioutil.ReadAll(resp.Body)
        log.WithChannel("222").Printf("%s",string(respBytes))
    
        if err != nil {
            log.WithChannel("curl post").Printf("url=%s,data=%s,result ioutil ReadAll error is %v", url, string(stringData), err)
            return nil, err
        }
        return respBytes, nil
    }
    

    2. 获取Content-Type:application/json;charset=UTF-8

    接收参数

    
    type TestS struct {
        OrderNo string `json:"order_no"`
        Name string `json:"name"`
    }
    
    func Register(c *gin.Context) {
        body, _ := ioutil.ReadAll(c.Request.Body) //josn数据
        test := &TestS{}
        _=json.Unmarshal(body,test)
    }
    

    post请求

    
    ## request.Header.Set("Content-Type", "application/json;charset=UTF-8")
    
    func CurlPost(byteData []byte, url string) ([]byte, error) {
        //body := strings.NewReader("order_no=cjb&name=xiao")
        body := bytes.NewReader(byteData)
        request, err := http.NewRequest("POST", url, body)
        if err != nil {
            log.WithChannel("curl post").Printf("url=%s,data=%s,http NewRequest error is %v", url, string(byteData), err)
            return nil, err
        }
        request.Header.Set("Content-Type", "application/json;charset=UTF-8")
    
        //使用连接池httpClient
        httpClient := &http.Client{}
        httpClient.Transport = &http.Transport{
            Proxy: http.ProxyFromEnvironment,
            DialContext: (&net.Dialer{
                Timeout:   30 * time.Second,
                KeepAlive: 30 * time.Second,
                DualStack: true,
            }).DialContext,
            MaxIdleConns:          100,
            MaxIdleConnsPerHost:   100,
            IdleConnTimeout:       90 * time.Second,
            TLSHandshakeTimeout:   90 * time.Second,
            ExpectContinueTimeout: 1 * time.Second,
        }
    
        resp, err := httpClient.Do(request)
        if err != nil {
            log.WithChannel("curl post").Printf("url=%s,data=%s,client Do request error is %v", url, string(byteData), err)
            return nil, err
        }
        respBytes, err := ioutil.ReadAll(resp.Body)
        log.WithChannel("222").Printf("%s",string(respBytes))
    
        if err != nil {
            log.WithChannel("curl post").Printf("url=%s,data=%s,result ioutil ReadAll error is %v", url, string(byteData), err)
            return nil, err
        }
        return respBytes, nil
    }
    

    相关文章

      网友评论

          本文标题:Go问题

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