简体   繁体   中英

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

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.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

// 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:

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

Expected Screen Output:

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

Problem: The Process p is created and printed successfully, but not added to currentGroup .

Question: What I know is that slice append function copies by value. Then, a Process object/struct is copied without the need of creating a slice of pointers. Hence the process struct is considered as an indivisible 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. 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).

You usually expose your interfaces and make the actual types unexported. Add a new method to create the type object.

Second if you want to modify the type object with the method call use pointer reference instead of value ie

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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