+ 收藏我们

网站模板

网站模板搜索
404模板 营销型模板 外贸网站模板 单页模板 双语模板 标签大全
电话:18630701785
首页 > 站长学院 > 嵌套结构属性未定义 >

嵌套结构属性未定义

时间:2024-04-06 14:52:34

本篇文章向大家介绍《嵌套结构属性未定义》,主要包括,具有一定的参考价值,需要的朋友可以参考一下。

问题内容
(之前链接的“答案”并未回答此问题。stackoverflow.com/questions/24809235/initialize-a-nested-struct。除非您能提供明确的答案,否则请不要关闭此问题。)

在此嵌套结构示例 testJSON 中,我收到错误 foo is undefined。

https://play.golang.com/p/jzGoifypnjz

不确定在 foo 属性的情况下使用 teststruct 赋值的正确方法是什么。

// TestStruct a test struct
type TestStruct struct {
    Foo struct {
        Thing string `json:Thing`
    } `json:Foo`
}

var testJSON = TestStruct{
    Foo: Foo{
        Thing: "test thing string",
    },
}

解决方案

尝试让 foo 成为它自己的结构。

package main

import (
    "fmt"
)

// TestStruct a test struct
type TestStruct struct {
    // you have to make the Foo struct by itself
    Foo
}

type Foo struct {
    Thing string
}

var testJSON = TestStruct{
    Foo: Foo{
        Thing: "test thing string",
    },
}

func main() {
    fmt.Println("Hello, playground")
}
如果你想要read about nested Structs, this might help。

到这里,我们也就讲完了《嵌套结构属性未定义》的内容了。

有问题可以加入网站技术QQ群一起交流学习

本站会员学习、解决问题QQ群(691961965)

客服微信号:lpf010888

pbootcms教程

织梦教程

站长学院

SEO

wordpress

竞价教程

信息流

Title