结构体之间的转换

zhaohao · 2017-04-28 06:48:33 · 3436 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2017-04-28 06:48:33 的主题,其中的信息可能已经有所发展或是发生改变。

将DBDiskGroupInfo 转换为DiskGroupInfo 还有跟快的吗???

package main

import (
    "fmt"
    "unsafe"
)

type DBDiskGroupInfo struct {
    ZoneID  int32   `bson:"ZoneID"`
    GroupID int32   `bson:"GroupID"`
    Status  int32   `bson:"Status"`
    Dids    []int32 `bson:"DidList"`
}
type DiskGroupInfo struct {
    ZoneID  uint32   `bson:"ZoneID"`
    GroupID uint32   `bson:"GroupID"`
    Status  uint32   `bson:"Status"`
    Dids    []uint32 `bson:"DidList"`
}

func (x *DBDiskGroupInfo) test3() (tmp DiskGroupInfo) {
    tmp.ZoneID = uint32(x.ZoneID)
    tmp.GroupID = uint32(x.GroupID)
    tmp.Status = uint32(x.Status)
    y := (*[]uint32)(unsafe.Pointer(&x.Dids))
    tmp.Dids = make([]uint32, len(x.Dids))
    copy(tmp.Dids, *y)
    return tmp
}
func main() {
    var x DBDiskGroupInfo
    x.ZoneID = 1
    x.GroupID = 2
    x.Status = 3
    x.Dids = make([]int32, 0)
    x.Dids = append(x.Dids, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 3, 4)

    y := x.test3()
    fmt.Println(y)
}

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

3436 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传