Create an object from nested struct definition in GoLang?

huangapple go评论84阅读模式
英文:

Create an object from nested struct definition in GoLang?

问题

有没有办法从嵌套的结构类型创建一个对象?

func main() {
	car := Car{}
	var wheel Car.Wheel
}

type Car struct {
	Wheel struct {
		name string
	}
}

我有一个深层嵌套的 JSON。我想单独操作许多这些嵌套的结构体。
我想通过“根”定义来访问结构体定义。类似于Car.Wheel,而不是在 JSON 中的许多嵌套对象中显式定义type Wheel struct

英文:

Is there a way to create an object from a nested struct type

func main() {
	car := Car{}
	var wheel Car.Wheel
}

type Car struct {
	Wheel struct {
		name string
	}
}

I have a deep nested json. I am interested in operating on many of these nested structs separately.
I want to access the struct definition through the “root” definition . Something like Car.Wheel instead of explicitly defining type Wheel struct for many of my nested objects in the json

答案1

得分: 2

有没有一种方法可以从嵌套的结构类型创建一个对象?

没有,因为没有所谓的“嵌套结构类型”。你没有一个类型叫做Car.Wheel,你有一个类型叫做Car,它有一个字段叫做Wheel;该字段的类型是一个未命名的结构体类型struct { name string }。你不能引用一个未命名的类型;它是没有名字的。要引用一个类型,你必须给它命名。你可以这样做:

var wheel struct { name string }

然后你就可以在wheelCar.Wheel之间进行赋值,因为它们是相同的类型;然而,这并不是特别方便(你必须在每次使用它的地方写出完整的类型定义),而且这意味着你不能在该类型上定义任何方法,这可能是你关心的一个限制。

一般来说,在Go语言中,你只需要为每个要使用的类型定义一个命名类型,这些定义应该位于顶层:

type Car struct {
    Wheel Wheel
}

type Wheel struct {
    name string
}
英文:

> Is there a way to create an object from a nested struct type

No, because there's no such thing as a "nested struct type". You don't have a type Car.Wheel, you have a type Car, with a field Wheel; that field's type is the unnamed type struct { name string }. You cannot refer to an unnamed type; it is unnamed. To refer to a type, you have to name it. You could do:

var wheel struct { name string }

And you'd be able to assign between wheel and Car.Wheel, because they're the same type; however, this is not particularly convenient (you'd have to write out the full type definition everywhere you use it), and it means you can't define any methods on the type, which may or may not be a limitation you care about.

Generally speaking, in Go, you just want to define a named type for each type you want to use, and those definitions would each be at the top level:

type Car struct {
    Wheel Wheel
}

type Wheel struct {
    name string
}

答案2

得分: 1

你是否在寻找类似这样的内容:

package main

import "fmt"

type Car struct {
	model string
	wheel Wheel
}

type Wheel struct {
	name string
}

func main() {
	car := Car{model: "Toyota"}
	car.wheel.name = "my wheel"

	fmt.Println("car model: ", car.model)
	fmt.Println("car wheel name:", car.wheel.name)
}
英文:

Are you looking for something like this:

package main

import "fmt"

type Car struct {
	model string
	wheel Wheel
}

type Wheel struct {
	name string
}

func main() {
	car := Car{model: "Toyota"}
	car.wheel.name = "my wheel"

	fmt.Println("car model: ", car.model)
	fmt.Println("car wheel name:", car.wheel.name)
}

huangapple
  • 本文由 发表于 2022年5月10日 08:01:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/72179357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定