Programming/golang
golang json/encoding marshal() unmarshal()
구차니
2022. 9. 28. 16:45
encoding/json 의 json.Unmarshal() 이나 json.Marshal()은 특이(?)하게도
구조체의 변수가 대문자여야 변환을 해준다.
어떤 버전이 그런 영향을 주는건진 모르겠지만.. 참고해야 할 듯..
package main import ( "net" // "net/http" "fmt" "encoding/json" _ "io" "os" ) const conf_file = "config.json" const server_url = "https://localhost" type Config struct { Server string } func main() { var config Config conf, err := os.ReadFile(conf_file) if err != nil { fmt.Println(err) fmt.Println("create default configuration file") config.Server = server_url jsonbyte, _ := json.Marshal(config) fmt.Println(config) fmt.Println(jsonbyte) os.WriteFile(conf_file, jsonbyte, 0644) } fmt.Println(conf) err = json.Unmarshal(conf, &config) fmt.Println(config) fmt.Println("*** Client start ***") temp, _ := net.InterfaceByName("enp0s25") mac := temp.HardwareAddr.String() fmt.Println(mac) } |
[링크 : https://www.joinc.co.kr/w/man/12/golang/json]
[링크 : https://jeonghwan-kim.github.io/dev/2019/01/18/go-encoding-json.html]
[링크 : http://golang.site/go/article/104-JSON-사용]