commit 36542fc159df03a0edfdeec88455fc59e9eecedc Author: flyfish <290198252@qq.com> Date: Thu Jul 23 13:59:39 2020 +0800 first commit diff --git a/network/net.go b/network/net.go new file mode 100644 index 0000000..c4b256a --- /dev/null +++ b/network/net.go @@ -0,0 +1,110 @@ +package network + +import ( + "bytes" + "io/ioutil" + "log" + "net" + "net/http" + "time" +) + +func HttpPut(url string,ct string,dat []byte,timeout time.Duration) ([]byte,error) { + req, _ := http.NewRequest("PUT", url, bytes.NewReader(dat)) + req.Header.Add("Content-Type", ct) + req.Header.Add("Date", "Wed, 12 Sep 2018 02:10:09 GMT") + log.Print(time.Now()) + client := &http.Client{ + Transport: &http.Transport{ + Dial: func(netw, addr string) (net.Conn, error) { + conn, err := net.DialTimeout(netw, addr, timeout) + if err != nil { + log.Print(err.Error()) + return nil, err + } + conn.SetDeadline(time.Now().Add(timeout)) + return conn, nil + }, + ResponseHeaderTimeout: timeout, + }, + } + res, e := client.Do(req) + if nil != e{ + return nil,e + } + defer res.Body.Close() + body, er := ioutil.ReadAll(res.Body) + return body,er +} +func HttpPost(url string,ct string,headers map[string] string, dat []byte,timeout time.Duration) ([]byte,error) { + req, _ := http.NewRequest("POST", url, bytes.NewReader(dat)) + req.Header.Add("Content-Type", ct) + req.Header.Add("Accept","*/*") + for k,v := range headers{ + req.Header.Add(k,v) + } + transport := &http.Transport{ + Dial: (&net.Dialer{ + Timeout: 10 * time.Second, + }).Dial, + MaxIdleConns: 2, + } + + + client := &http.Client{ + Transport: transport, + Timeout: timeout, + } + + resp, err := client.Do(req) + if err != nil { + log.Print(err.Error()) + return nil,err + } + + defer func() { + e := resp.Body.Close() + if nil != e{ + log.Print(e.Error()) + return + } + }() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Print(err.Error()) + + return nil,err + } + return body,nil +} +// HttpGet get请求 +func HttpGet(url string,timeout time.Duration) ([]byte,error) { + client := &http.Client{ + Transport: &http.Transport{ + Dial: func(netw, addr string) (net.Conn, error) { + conn, err := net.DialTimeout(netw, addr, timeout) + if err != nil { + log.Print(err.Error()) + return nil, err + } + conn.SetDeadline(time.Now().Add(timeout)) + return conn, nil + }, + ResponseHeaderTimeout: timeout, + }, + } + resp, err := client.Get(url) + if err != nil { + log.Print(err.Error()) + return nil,err + } + + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Print(err.Error()) + + return nil,err + } + return body,nil +} \ No newline at end of file diff --git a/utils/code_help.go b/utils/code_help.go new file mode 100644 index 0000000..8c2cb7c --- /dev/null +++ b/utils/code_help.go @@ -0,0 +1,88 @@ +package go_utils + +import ( + "reflect" + "strings" + "encoding/json" + "log" +) + +// 驼峰转蛇形 + func ToSnakeString(s string) string { + data := make([]byte, 0, len(s)*2) + j := false + num := len(s) + for i := 0; i < num; i++ { + d := s[i] + if i > 0 && d >= 'A' && d <= 'Z' && j { + data = append(data, '_') + } + if d != '_' { + j = true + } + data = append(data, d) + } + return strings.ToLower(string(data[:])) +} + +// 蛇形转驼峰 +func ToCamelString(s string) string { + data := make([]byte, 0, len(s)) + j := false + k := false + num := len(s) - 1 + for i := 0; i <= num; i++ { + d := s[i] + if k == false && d >= 'A' && d <= 'Z' { + k = true + } + if d >= 'a' && d <= 'z' && (j || k == false) { + d = d - 32 + j = false + k = true + } + if k && d == '_' && num > i && s[i+1] >= 'a' && s[i+1] <= 'z' { + j = true + continue + } + data = append(data, d) + } + return string(data[:]) +} + + +//递归的语义化分析 +// todo 目前只做单层,以后支持多层 +func GetStructCodeFromJson(s string,structName string) string { + gencode := "type " + structName + " struct {\r\n" + dat := map[string]interface{}{} + e := json.Unmarshal([]byte(s),&dat) + if nil != e{ + log.Print(e.Error()) + return "" + } + for k,v := range dat{ + log.Print(k , " ",reflect.TypeOf(v).Kind().String()) + //如果是切片,切片类型 + if reflect.TypeOf(v).Kind() == reflect.Slice{ + gencode += " " + ToCamelString(k) + " " + reflect.TypeOf(v).Kind().String() + " `json:\"" + k + "\"` \r\n" + log.Print(reflect.ValueOf(v).Slice(0,0).Kind()) + //reflect.ValueOf(v).Slice(0,1) + // todo 先暂时用interface代替 + continue + } + // 如果是字典 + if reflect.TypeOf(v).Kind() == reflect.Map{ + gencode += " " + ToCamelString(k) + " map[string]interface{} " + " `json:\"" + k + "\"` \r\n" + + for _ ,v := range reflect.ValueOf(v).MapKeys(){ + log.Print(v) + } + continue + } + gencode += " " + ToCamelString(k) + " " + reflect.TypeOf(v).Kind().String() + " `json:\"" + k + "\"` \r\n" + // 基础类型直接加代码 + } + gencode += "}" + return gencode +} \ No newline at end of file diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..f6f5588 --- /dev/null +++ b/utils/utils.go @@ -0,0 +1,38 @@ +package utils + +import ( + "crypto/md5" + "encoding/hex" + "reflect" + "strings" +) + +// GetMd5ToLow Md5加密小写 +func GetMd5ToLow(str string) string { + m := md5.New() + m.Write([]byte(str)) + result := hex.EncodeToString(m.Sum(nil)) + result = strings.ToLower(result) + return result +} +// InArrary 是否在数组 +func InArrary(arr interface{},ele interface{}) int{ + if reflect.TypeOf(arr).Kind() != reflect.Slice && reflect.TypeOf(arr).Kind() != reflect.Array{ + return -1 + } + //数组为空 + if reflect.ValueOf(arr).Len() == 0{ + return 1 + } + // 数组和元素不一致 + if reflect.ValueOf(arr).Index(0).Type().Kind() != reflect.TypeOf(ele).Kind() { + return 1 + } + for i := 0; i < reflect.ValueOf(arr).Len();i ++ { + if reflect.ValueOf(arr).Index(i).Interface() == + reflect.ValueOf(ele).Interface(){ + return 0 + } + } + return 1 +} \ No newline at end of file