简体   繁体   中英

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!

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)
}

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