go语言 结构体和Json

package main

import (
   "encoding/json"
   "fmt"
   "net/http"
)

//将int定义为 myInt型
type myInt int

// 为MyInt添加IsZero()方法
func (m myInt) IsInt() bool {
   return m == 0
}

// 为MyInt添加Add()方法
func (m myInt) addInt(other int) int {
   return other + int(m)
}

//使用事件系统实现事件的响应和处理
type class struct {
}

//给结构体添加一个 do方法
func (c *class) Do(v int) {
   fmt.Println("This is", v)
}

//普通函数的Do
func funcDo(v int) {
   fmt.Println("函数中的===》", v)
}

//内嵌和结构体内嵌
//结构体可以包含一个或者多个匿名(或内嵌)字段 即这些字段没有显示名字只有字段类型是必须的 此时类型也就是字段的名字
//匿名字段本身可以是一个结构体类型 即 结构体可以包含内嵌结构体

type innerS struct {
   int1 int
   int2 int
}

type other struct {
   b int
   c float32
   int
   innerS
}

type A struct {
   sa, ay int
}
type B struct {
   A
   bx, by float32
}

//结构体内嵌模拟类的继承
//可飞行的
type Flying struct {
}

func (f *Flying) Fly(name string) {
   fmt.Println(name, "I Can fly")
}

//可行走的
type Walkble struct {
}

func (w *Walkble) Walk(name string) {
   fmt.Println(name, "I can Walk")
}

//人类
type Man struct {
   Walkble
}

//鸟类
type Bird struct {
   Walkble
   Flying
}

//初始化内嵌结构体
//例子  车辆的组装和初始化
//车轮
type Wheel struct {
   Size int
}

//引擎
type Engine struct {
   Power int
   Type  string
}

//车
type Car struct {
   Wheel
   Engine
}

//使用匿名结构体解析json数据
//例子 手机为例
//定义手机屏幕
type Screen struct {
   Size       float32
   ResX, ResY int
}

//定义电池
type Battery struct {
   Capacity int
}

//生成json数据
func genJson() []byte {
   raw := &struct {
      Screen
      Battery
      HasTouchID bool
   }{
      Screen: Screen{
         Size: 5.5,
         ResX: 1920,
         ResY: 1080,
      },
      // 电池参数
      Battery: Battery{
         2910,
      },
      // 是否有指纹识别
      HasTouchID: true,
   }
   // 将数据序列化为json
   jsonData, _ := json.Marshal(raw)
   return jsonData
}

//将结构体利的数据 转换为 json
//在转换 JSON 格式时,JSON 的各个字段名称默认使用结构体的名称,
//如果想要指定为其它的名称我们可以在声明结构体时添加一个`json:" "`标签,在" "中可以填入我们想要的内容
//声明技能结构体
type Skill struct {
   Name  string `json:"name"`
   Level int
}

//声明角色结构体
type Actor struct {
   Name   string
   Age    int
   Skills []Skill
}

func main() {

   //为任意类型添加方法
   //使用type关键字可以定义出新的自定义模型之后可以为自定义类型添加各种方法了
   var b myInt
   fmt.Println(b.IsInt())
   b = 2
   fmt.Println(b.addInt(2))

   //httP 包中的类型方法
   client := &http.Client{}
   req, err := client.Get("http://www.baidu.com")
   if err != nil {
      fmt.Println(err)
      return
   }
   fmt.Println(req.Status)
   class := new(class)
   class.Do(1)

   //内嵌和结构体内嵌
   outer := new(other)
   outer.b = 2
   outer.c = 1.2
   outer.int = 60
   outer.int1 = 20
   outer.int2 = 30
   fmt.Println(outer)

   c := new(B)
   c.ay = 1
   c.bx = 2
   c.by = 3
   c.sa = 4
   fmt.Println(c)

   //实例化鸟类
   bird := new(Bird)
   bird.Fly("鸟")
   bird.Walk("鸟")

   //实例化人类
   man := new(Man)
   man.Walk("男人")

   //初始化内嵌结构体
   car := Car{
      Wheel:  Wheel{Size: 4},
      Engine: Engine{Power: 13, Type: "13T"},
   }
   fmt.Println(car)

   //对json 字符串的处理
   jsonData := genJson()
   fmt.Println(string(jsonData))

   // 填充基本角色数据
   Actior := Actor{
      Name: "cow boy",
      Age:  37,
      Skills: []Skill{
         {Name: "Roll and roll", Level: 1},
         {Name: "Flash your dog eye", Level: 2},
         {Name: "Time to have Lunch", Level: 3},
      },
   }
   fmt.Println(Actior)
   //转换为json
   result, err_1 := json.Marshal(Actior)
   if err_1 != nil {
      fmt.Println("error:", err)
   }
   fmt.Println(string(result))
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章