简体   繁体   English

如何使用切片构造嵌套结构

[英]How to construct nested structs with slices

I am building a simple group manager in which I share with you the relevant part only.我正在构建一个简单的组管理器,我只与您分享相关部分。 I am having a trouble with understanding how the things work in Object-like structs without constructors (and copy constructors in particular).我很难理解在没有构造函数(尤其是复制构造函数)的类对象结构中事物是如何工作的。

main.go主.go

var currentGroup Headers.Group

func main() {

    currentGroup.New(0)

    p := Headers.Process{ID: uint16(len(currentGroup.Processes)), Address: "Test"}
    currentGroup.AddProcess(p)
    p.PrintProcess()
    currentGroup.PrintGroup()
}

I have a class-like called Process我有一个名为 Process 的类

Process.go制程.go

//Process defines a Process structure
type Process struct {
    ID      uint16
    Address string
}

type processFuncs interface {
    printProcess()
}

//PrintProcess prints a process contents
func (p Process) PrintProcess() {

    fmt.Printf("[P%d; %s]\n", p.ID, p.Address)
}

and a group class-like和一个类

Group.go组.go

// Group struct
type Group struct {
    ID        uint16
    Processes []Process
}

//AddProcess adds a process to the group
func (G Group) AddProcess(p Process) {

    G.Processes = append(G.Processes, p)
}

//PrintGroup prints the group processes
func (G Group) PrintGroup() {

    fmt.Printf("=========================\nGroup %d:\n", G.ID)
    for i := 0; i < len(G.Processes); i++ {
        G.Processes[i].PrintProcess()
    }
}

//New Group Constructor
func (G Group) New(ID uint16) {
    G.ID = ID
    G.Processes = make([]Process, 0)
}

Screen Output:屏幕Output:

[P0; Test]
=========================
Group 0:

Expected Screen Output:预期屏幕 Output:

[P0; Test]
=========================
Group 0:
[P0; Test]

Problem: The Process p is created and printed successfully, but not added to currentGroup .问题: Process p已成功创建并打印,但未添加到currentGroup

Question: What I know is that slice append function copies by value.问题:我所知道的是,切片append function 按值复制。 Then, a Process object/struct is copied without the need of creating a slice of pointers.然后,无需创建指针切片即可复制Process对象/结构。 Hence the process struct is considered as an indivisible object.因此,进程结构被认为是不可分割的 object。 Is that true?真的吗? Any misunderstanding?有什么误会吗? Anything wrong with the code?代码有什么问题吗?

Here I'll drop some hints for you.在这里,我将为您提供一些提示。 Which are mostly my learning when i started using golang.当我开始使用 golang 时,这主要是我的学习。 Please match not only the syntax but also the casing.请不仅匹配语法,还要匹配大小写。

type processFuncs interface {
    **printProcess**()
}

//PrintProcess prints a process contents
func (p Process) **PrintProcess**() {

    fmt.Printf("[P%d; %s]\n", p.ID, p.Address)
}

another important point: functions, methods or variables/members starting with Capital letter are exported (same as Public), starting in small case un-exported(more like private or better package level access).另一个重点:导出以大写字母开头的函数、方法或变量/成员(与公共相同),以小写开头未导出(更像私有或更好的 package 级别访问)。

You usually expose your interfaces and make the actual types unexported.您通常会公开您的接口并使实际类型不被导出。 Add a new method to create the type object.添加一个新方法来创建类型 object。

Second if you want to modify the type object with the method call use pointer reference instead of value ie其次,如果您想使用方法调用修改类型 object 使用指针引用而不是值,即

Use func (p *Process) PrintProcess(){
//... do something
}

instead of代替

func (p Process) PrintProcess(){
//... do something
}

Most importantly Go is not like Java or C# or any other OO language so don't write OO code in golang.最重要的是 Go 不像 Java 或 C# 或任何其他 OO 语言,所以不要在 golang 中编写 OO 代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM