将类型* [] foo的变量转换为* [] bar

问题描述 投票:1回答:1
type foo struct {
    Field1 int
    Field2 string
}

type bar struct {
    Field1 int
    Field2 string
}

func main() {
    x := foo{1, "Hello"}
    y := bar(x)

    a := [...]foo{x, x}
    b := a[:]

    c := (*[]bar)(&b)

    fmt.Println(x, y, a, b, c)
}

我想在两个相同的结构之间转换。主要是在两个结构上使用不同的json标签。有没有办法做到这一点?我已经尝试了上面的示例以及带有指针切片而不是指向切片的指针的示例。无济于事。

go struct slice
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.