Created
March 8, 2018 16:54
-
-
Save s4l1h/2893d8f5b2560ceac383b88bdc7caf82 to your computer and use it in GitHub Desktop.
Golang JSON ve interface kullanımı.(v3)
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" | |
) | |
// Person object | |
type Person struct { | |
FirstName, LastName string // Küçük harf olursa json kütüphanesi erişemeyecek. | |
Number string `json:"number"` // json field name'i değişebiliriz. | |
Company *Company | |
} | |
// Company object | |
type Company struct { | |
Name string `json:"name"` // Şirket Adı | |
Address string // Şirket Adresi | |
Persons []*Person `json:"persons"` // Personeller | |
} | |
func main() { | |
company := &Company{ | |
Name: "AKM", | |
Address: "TeknoKent", | |
} | |
p := &Person{ | |
FirstName: "A.Kadir", | |
LastName: "Mutlu", | |
} | |
p.Number = "0449444944944" | |
p.Company = company // Bu Satırı eklememiz hataya sebep olacak. | |
company.Persons = append(company.Persons, p) | |
result, err := json.Marshal(company) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(string(result)) | |
/* | |
~/go/src/github.com/s4l1h/jsonTest v3 ● go run main.go | |
runtime: goroutine stack exceeds 1000000000-byte limit | |
fatal error: stack overflow | |
runtime stack: | |
runtime.throw(0x10e0fb1, 0xe) | |
/usr/local/Cellar/go/1.10/libexec/src/runtime/panic.go:619 +0x81 | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment