Last active
July 11, 2023 07:53
-
-
Save stnc/ba1af4b50658199316478a0e01b8eee0 to your computer and use it in GitHub Desktop.
embed strcut and pointer example 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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