Skip to content

Instantly share code, notes, and snippets.

@s4l1h
Created March 8, 2018 16:48
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 s4l1h/a36e0cc33f5735d32501fa26019308e6 to your computer and use it in GitHub Desktop.
Save s4l1h/a36e0cc33f5735d32501fa26019308e6 to your computer and use it in GitHub Desktop.
Golang JSON ve interface kullanımı.(v2)
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 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"
company.Persons = append(company.Persons, p)
result, err := json.Marshal(company)
if err != nil {
panic(err)
}
fmt.Println(string(result))
/*
{
"name":"AKM",
"Address":"TeknoKent",
"persons":[
{
"FirstName":"A.Kadir",
"LastName":"Mutlu",
"number":"0449444944944"
}
]
}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment