shazbot
shazbot

Reputation: 585

Assign values to a slice struct in go ( golang )

How can I assign values to a var of type []struct ?

type Mappings []struct {
    PropA       string  
    PropB       string 
}

func main() {
    var test Mappings
    test = ???
}

Thanks in advance!

Upvotes: 1

Views: 3102

Answers (1)

mattn
mattn

Reputation: 7723

package main

import (
    "fmt"
)
type Mappings []struct {
    PropA       string  
    PropB       string 
}

func main() {
    var test Mappings
    test = Mappings{
        {PropA: "foo", PropB: "bar"},
        {PropA: "bar", PropB: "baz"},
    }
    fmt.Println(test)
}

Upvotes: 5

Related Questions