Abhinav
Abhinav

Reputation: 1015

Setter method for slice struct in golang

I have an strut defined as below

type ReqJSON struct {
    Request struct {
        Data struct {
            Object struct {
                User []User `json:"user"`
            } `json:"object"`
        } `json:"data"`
    } `json:"request"`
}

type User struct {
    UserID string `json:"userid"`
    IPAddr string `json:"ipaddr"`
    Noofusers string `json:"noofusers"`
    Qos string `json:"qos"`
    ID string `json:"id"`
    Domain string `json:"domain"`
}

The setter function for this struct is as below

func (j *ReqJSON) User() User     { return j.Request.Data.Object.User } 
func (j *ReqJSON) SetUser(u User) { j.Request.Data.Object.User = u }

The main program is as follows

func main() {
    var j ReqJSON
    j.SetUser(User{
            UserID:    "_id",
            IPAddr:    "1.1.1.1",
            Noofusers: "100",
            Qos:       "34",
            ID:        "kldjflkdfjlkdjfkld",
            Domain:    "xyz.com",
    })

    b, err := json.MarshalIndent(j, "", "  ")
    fmt.Println(err, string(b))
}

When I tried to make User as slice I am getting error for the above function.

prog.go:27:65: cannot use j.Request.Data.Object.User (type []User) as type User in return argument prog.go:28:64: cannot use u (type User) as type []User in assignment

I am novice to Golang so not able to figure out how I can make Setter and Ctor to return slice object.

Upvotes: 1

Views: 1367

Answers (2)

Himanshu
Himanshu

Reputation: 12675

It is showing an error because ReqJSON struct contains a field for User which is a slice of User struct but when you are setting the Value for User struct you are not passing slice of User struct inside setter function.

type ReqJSON struct {
    Request struct {
        Data struct {
            Object struct {
                User []User `json:"user"` // this is a slice of User struct
            } `json:"object"`
        } `json:"data"`
    } `json:"request"`
}

You need to pass a slice of User struct in Setter function if you want to initialize the field inside ReqJSON struct which contains slice of User not User. Pass slice of User struct inside main as:

var u []User
u = append(u, User{
    UserID:    "_id",
    IPAddr:    "1.1.1.1",
    Noofusers: "100",
    Qos:       "34",
    ID:        "kldjflkdfjlkdjfkld",
    Domain:    "xyz.com",
})
j.SetUser(u)

Working Code on Go Playground

Upvotes: 0

Mad Wombat
Mad Wombat

Reputation: 15105

If you change the single embedded user struct to a list of users you need to change it everywhere. Golang is strongly typed and a slice of users is a different type from a single user struct. The following code works

package main

import (
    "fmt"
    "encoding/json"
)

type ReqJSON struct {
    Request struct {
        Data struct {
            Object struct {
                User []User `json:"user"`
            } `json:"object"`
        } `json:"data"`
    } `json:"request"`
}

type User struct {
    UserID    string `json:"userid"`
    IPAddr    string `json:"ipaddr"`
    Noofusers string `json:"noofusers"`
    Qos       string `json:"qos"`
    ID        string `json:"id"`
    Domain    string `json:"domain"`
}

func (j *ReqJSON) User() []User     { return j.Request.Data.Object.User } 
func (j *ReqJSON) SetUsers(u []User) { j.Request.Data.Object.User = u }

func main() {

    var j ReqJSON
    j.SetUsers([]User{
    User{
        UserID:    "_id",
        IPAddr:    "1.1.1.1",
        Noofusers: "100",
        Qos:       "34",
        ID:        "kldjflkdfjlkdjfkld",
        Domain:    "xyz.com",
    },
    })

    b, err := json.MarshalIndent(j, "", "  ")
    fmt.Println(err, string(b))
}

Upvotes: 2

Related Questions