golang学习笔记

1、interface 转 string,int,float64
func interface2String(inter interface{}) {

    switch inter.(type) {

    case string:
        fmt.Println("string", inter.(string))
        break
    case int:
        fmt.Println("int", inter.(int))
        break
    case float64:
        fmt.Println("float64", inter.(float64))
        break
    }

}

func main() {
    interface2String("jack")
    interface2String(1)
    interface2String(12.223)
}

2、运用反射查看变量数据类型

import (
    "fmt"
    "reflect"
)
fmt.Println("type:", reflect.TypeOf(x))

 3、json字符串转json对象然后转集合+类型断言访问

a、字符串直接解析

  1 package main
  2 
  3 import (
  4     "fmt"
  5     "encoding/json"
  6     "reflect"
  7 )
  8 
  9 func main(){
 10     newPostData := make(map[string]interface{})
 11     var postData = `{"id":123}`
 12     // 字符串转字符串集合
 13     b := []byte(postData)
 14     err := json.Unmarshal(b, &newPostData)
 15     if err != nil {
 16         
 17     }
 18     fmt.Println("type:", reflect.TypeOf(newPostData))
 19     /*输出json */
 20     for aa := range newPostData {
 21         fmt.Println("type:", reflect.TypeOf(newPostData[aa]))
 22         fmt.Println(newPostData[aa])
 23     }
 24     // json字符串转map集合
 25     new_json_two := make(map[string]interface{})
 26     // 定json,由此可以看出会被解析为一个 map[string]interface{} ,首先获取耳机
 27     json_two := `{"type":[{"id":123,"name":"jack"}]}`
 28     err = json.Unmarshal([]byte(json_two), &new_json_two)
 29     if err != nil {
 30     }
 31     /*输出json */
 32     for idx := range new_json_two {
 33         fmt.Println(new_json_two[idx])
 34         fmt.Println("new_json_two type is:", reflect.TypeOf(new_json_two[idx]))//数组
 35         // []interface{}
 36            fmt.Println(new_json_two[idx])
 37            // 输出
 38         switch vv := new_json_two[idx].(type) {
 39             case string:
 40                 // 字符串
 41                 fmt.Println(vv)
 42             case float64:
 43                 // 数字
 44                 fmt.Println(vv)
 45             case bool:
 46                 // 布尔值
 47                 fmt.Println(vv)
 48             case map[string]interface{}:
 49                 // 集合
 50                 fmt.Println(vv)
 51                 for i, j := range vv {
 52                     fmt.Println(i,":",j)
 53                 }
 54             case []interface{}:
 55                 // 数组
 56                 fmt.Println(vv)
 57                 for i, j := range vv {
 58                     fmt.Println(i, ":", j)
 59                     fmt.Println("type:", reflect.TypeOf(j))
 60                     fmt.Println(j)
 61                     showDetail(j)
 62                     // interface转为map
 63                     var tmp = j.(map[string]interface{})
 64                        for ii,jj := range tmp {
 65                         fmt.Println(jj,tmp[ii])
 66                     }
 67                 }
 68             default:
 69                 // nil
 70                 fmt.Println(vv)
 71         }
 72     }
 73 }
 74 
 75 func showDetail(item interface{}){
 76     switch vv := item.(type) {
 77         case string:
 78             // 字符串
 79             fmt.Println(vv)
 80         case float64:
 81             // 数字
 82             fmt.Println(vv)
 83         case bool:
 84             // 布尔值
 85             fmt.Println(vv)
 86         case map[string]interface{}:
 87             // 集合
 88             fmt.Println(vv)
 89             for i, j := range vv {
 90                 fmt.Println("map[string]interface{}",i,j)
 91                 fmt.Println(vv[i])
 92             }
 93         case []interface{}:
 94             // 数组
 95             fmt.Println(vv)
 96             for i, j := range vv {
 97                 fmt.Println(i, ":", j)
 98                 fmt.Println("type:", reflect.TypeOf(j))
 99                 fmt.Println(j)
100                 // for ii := range j {
101                     // fmt.Println(j[ii].(string))
102                 // }
103             }
104         default:
105             // nil
106             fmt.Println(vv)
107     }
108 }
View Code

b、json文件读取后解析

{
    "id": 1,
    "content": "hello world",
    "author": {
        "id": 2,
        "name": "userA"
    },
    "published": true,
    "label": [],
    "nextPost": null,
    "comments": [{
            "id": 3,
            "content": "good post1",
            "author": "userB"
        },
        {
            "id": 4,
            "content": "good post2",
            "author": "userC"
        }
    ]
}
View Code
package main

import (
    "fmt"
    "encoding/json"
    "os"
    "io/ioutil"
)

func main() {
    // 读取json数据
    fh, err := os.Open("a.json")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer fh.Close()
    jsonData, err := ioutil.ReadAll(fh)
    if err != nil {
        fmt.Println(err)
        return
    }
    
    // 解析json数据到interface{}
    var unknown interface{}
    err = json.Unmarshal(jsonData, &unknown)
    if err != nil {
        fmt.Println(err)
        return
    }
 
    // 进行断言,并switch匹配
    m := unknown.(map[string]interface{})
    for k, v := range m {
        switch vv := v.(type) {
        case string:
            // 字符串
            fmt.Println(k, ":", vv)
        case float64:
            // 数字
            fmt.Println(k, "", vv)
        case bool:
            // 布尔值
            fmt.Println(k, ":", vv)
        case map[string]interface{}:
            // 集合
            fmt.Println(k, ":", vv)
            for i, j := range vv {
                fmt.Println(i,": ",j)
            }
        case []interface{}:
            // 数组
            fmt.Println(k, "", vv)
            for key, value := range vv {
                fmt.Println(key, ": ", value)
            }
        default:
            // nil
            fmt.Println(k, ":", vv)
        }
    }
}
View Code

参考地址    https://blog.csdn.net/qq_36178899/article/details/84869079

 4、golang笔记 - strconv的用法

 https://juejin.im/post/5c18d0c2e51d45270f53fc1d

 https://www.cnblogs.com/yaowen/p/8353444.html

5、格式转换

比如 float64 的整数转为整数字符串

strconv.FormatFloat(item["id"].(float64), 'f', -1, 64)

https://www.jb51.net/article/119164.htm

 

posted @ 2019-12-20 16:06  许伟强  阅读(359)  评论(0编辑  收藏  举报