I have this type with a value member called "Url". When encoding this type to json, I want to prepend the HOST value in front of the Url. What is the best way to achieve that?
In this example, the Println
statement prints {"url":"/thisurl"}
, I want it to print {"url":"http://myhost.com/thisurl"}
package main
import "fmt"
import "encoding/json"
type Post struct {
Url string `json:"url"`
}
const (
HOST = "http://myhost.com"
)
func main() {
post := Post{"/thisurl"}
marshaled, _ := json.Marshal(post)
fmt.Println(string(marshaled))
//{"url":"/thisurl"}
}
Update
I could re-assign post.Url right before the json.Marshal
line
post.Url = fmt.Sprintf("%s%s", HOST, post.Url)
But that feels a little messy, if I have to remember to do a reassignment every time I want to encode to json.
I don't want to change the value of post.Url
, I simply want to change how it is represented as json.