Skip to content

Instantly share code, notes, and snippets.

@stnc
Last active July 11, 2023 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stnc/ba1af4b50658199316478a0e01b8eee0 to your computer and use it in GitHub Desktop.
Save stnc/ba1af4b50658199316478a0e01b8eee0 to your computer and use it in GitHub Desktop.
embed strcut and pointer example 3
package main
import (
"encoding/json"
"fmt"
"log"
)
type MyResponseRoot struct {
Version string `json:"version,omitempty"`
Text string `json:"text,omitempty"`
}
type Server struct {
Name string `json:"name,omitempty"`
MyResponseRoot
}
// setName if * pointer = ok
func (server *Server) setData(newName string, version string) {
server.Name = newName
server.Version = version
}
func main() {
fmt.Println("/////---------- example 1")
server := Server{
Name: "John",
}
fmt.Println(server.Name)
fmt.Println("/////---------- example 2")
co := Server{
MyResponseRoot: MyResponseRoot{
Version: "1",
},
Name: "some name",
}
fmt.Println(co.Version) //result 1
//fmt.Println(co.name)
fmt.Println(server.Version) // result ""
fmt.Println("/////---------- example 3")
server.setData("Jack", "3")
fmt.Println(server.Name)
fmt.Println(server.Version) // result 3
empJSON, err := json.MarshalIndent(server.MyResponseRoot, "", " ")
if err != nil {
log.Fatalf(err.Error())
}
fmt.Printf("MarshalIndent funnction output\n %s\n", string(empJSON))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment