Go-List(Cont)

List例子。

package main

import (
    "container/list"
    "encoding/json"
    "fmt"
)

type Employee struct {
    Name string
    City string
}

func (e *Employee) String() string {
    return fmt.Sprintf("Employee[address: %p, Name: %s, City: %s]", e, e.Name, e.City)
}

func foo() {
    employees := list.New()
    for i := 1; i <= 5; i++ {
        employees.PushBack(&Employee{fmt.Sprintf("name%d", i), fmt.Sprintf("city%d", i)})
    }

    for e := employees.Front(); e != nil; e = e.Next() {
        fmt.Println(e)
    }

    json_string, err := json.Marshal(employees)
    if err != nil {
        fmt.Println("JSON failed: ", err)
    } else {
        fmt.Printf("JSON: %s\n", json_string)
    }

    json_string, err = json.Marshal(employees.Front().Value)
    if err != nil {
        fmt.Println("JSON failed: ", err)
    } else {
        fmt.Printf("JSON: %s\n", json_string)
    }

    var sliceEmployees []*Employee
    list2slice(employees, &sliceEmployees)
    json_string, err = json.Marshal(sliceEmployees)

    if err != nil {
        fmt.Println("JSON failed: ", err)
    } else {
        fmt.Printf("JSON: %s\n", json_string)
    }
}

func list2slice(listEmployees *list.List, sliceEmployees *[]*Employee) {
    *sliceEmployees = make([]*Employee, 0, listEmployees.Len())
    for e := listEmployees.Front(); e != nil; e = e.Next() {
        if employee, ok := e.Value.(*Employee); ok {
            *sliceEmployees = append(*sliceEmployees, employee)
        } else {
            fmt.Println("Type cast error.")
        }
    }
}

/*
&{0xc042050180 0xc042050120 0xc042050120 Employee[address: 0xc042004660, Name: name1, City: city1]}
&{0xc0420501b0 0xc042050150 0xc042050120 Employee[address: 0xc042004680, Name: name2, City: city2]}
&{0xc0420501e0 0xc042050180 0xc042050120 Employee[address: 0xc0420046a0, Name: name3, City: city3]}
&{0xc042050210 0xc0420501b0 0xc042050120 Employee[address: 0xc0420046c0, Name: name4, City: city4]}
&{0xc042050120 0xc0420501e0 0xc042050120 Employee[address: 0xc0420046e0, Name: name5, City: city5]}
JSON: {}
JSON: {"Name":"name1","City":"city1"}
JSON: [{"Name":"name1","City":"city1"},
       {"Name":"name2","City":"city2"},
       {"Name":"name3","City":"city3"},
       {"Name":"name4","City":"city4"},
       {"Name":"name5","City":"city5"}]
*/
func main() {
    foo()
}

注:List对象生成的JSON串为空,因为其成员函数都是小写字母开头的,无法导出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值