qzvrwxce
qzvrwxce

Reputation: 11

Extending Unnamed Types in Go

So the following works:

type Individual [][]int
type Population []*Individual

What I'm trying to do is add a field to Population so I do the following

var p Population
p.Name = "human"

So I tried this:

type Individual [][]int
type Population struct {
     []*Individual
     Name string
}

But it doesn't work for me. How do I do this?

Upvotes: 0

Views: 58

Answers (1)

Roman Kiselenko
Roman Kiselenko

Reputation: 44360

You should declare a name for the field of your struct:

package main

import (
    "fmt"
)

type Individual [][]int

type Population struct {
    Individual []*Individual // <- A name for field
    Name       string
}

func main() {
    var p Population
    p.Name = "human"
    fmt.Printf("%+v", p)
}

playground

=> {Individual:[] Name:human}

Upvotes: 1

Related Questions