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))
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章