dtwknzk3764 2014-07-08 21:53
浏览 338
已采纳

Golang:结构切片之间的类型转换

This questions follows another question of mine.

I don't exactly get what is wrong with my attempt to convert res to a ListSociete in the following test code :

import (
    "errors"
    "fmt"
    "github.com/jmcvetta/neoism"
)

type Societe struct {
    Name string
}

type ListSociete []Societe

func loadListSociete(name string) (ListSociete, error) {
    db, err := neoism.Connect("http://localhost:7474/db/data")
    if err != nil {
        return nil, err
    }
    res := []struct {
        Name string `json:"a.name"`
    }{}
    cq := neoism.CypherQuery{
        Statement: `
            MATCH (a:Societe)
            WHERE a.name = {name}
            RETURN a.name
            `,
        Parameters: neoism.Props{"name": name},
        Result:     &res,
    }
    db.Cypher(&cq)
    if len(res) == 0 {
        return nil, errors.New("Page duz not exists")
    }
    r := res[0]
    return ListSociete(res), nil
}

Is a []struct{Name string} different from a []struct{Name string json:"a.name" } ?

Or is a ListSociete different from a []struct{Name string} ?

Thanks.

  • 写回答

2条回答 默认 最新

  • dplht39359 2014-07-08 22:49
    关注

    You are currently dealing with two different types:

    type Societe struct {
        Name string
    }
    

    and the anonymous one:

    struct {
        Name string `json:"a.name"`
    }
    

    These two would be identical if it wasn't for the tag. The Go Specifications states (my emphasis):

    Two struct types are identical if they have the same sequence of fields, and if corresponding fields have the same names, and identical types, and identical tags. Two anonymous fields are considered to have the same name. Lower-case field names from different packages are always different.

    So, you can't do a simple conversion between the two. Also, the fact that you are converting slices of the two types makes the conversion problematic. I can see two options for you:

    Copy through iteration:

    This is the safe and recommended solution, but it is also more verbose and slow.

    ls := make(ListSociete, len(res))
    for i := 0; i < len(res); i++ { 
        ls[i].Name = res[i].Name
    }
    return ls, nil
    

    Unsafe conversion:

    Since both types have the same underlying data structure, it is possible to do an unsafe conversion.
    This might however blow up in your face later on. Be warned!

    return *(*ListSociete)(unsafe.Pointer(&res)), nil
    

    Playground Example: http://play.golang.org/p/lfk7qBp2Gb

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵