Home > OS >  Go - building POST body, receive error "invalid composite literal type string"
Go - building POST body, receive error "invalid composite literal type string"

Time:06-04

I'm trying to build a POST body in Go, but I keep getting the following error:

invalid composite literal type string

Snippets of my code and structs are below, I'm can't figure out what I'm doing wrong?

postData := projectPostData{
    Filters: projectFilters{
        Name: string{ // <-- Error is referred to on this line 
            target,
        },
    },
}

type projectPostData struct {
    Filters projectFilters `json:"filters,omitempty"`
}

type projectFilters struct {
    Name string `json:"name,omitempty"`
}

CodePudding user response:

You can check the below code:

package main 

import(
    "fmt"
)


type projectFilters struct {
    Name string `json:"string,omitempty"`
}

type projectPostData struct {
    Filters projectFilters `json:"filters,omitempty"`
}



func main(){
    target := "test target"

    postData := projectPostData{
        Filters: projectFilters{
            Name: target,
        },
    }
    
    fmt.Println(postData)
}
  •  Tags:  
  • go
  • Related