move: structs to root
This commit is contained in:
@ -7,14 +7,13 @@
|
||||
[x] struct fields
|
||||
[x] compare and assign
|
||||
[x] printing
|
||||
[ ] embedding
|
||||
[x] embedding
|
||||
[ ] exporting struct and fields
|
||||
[ ] struct tags {json encode/decode} - project?
|
||||
|
||||
**LATER:**
|
||||
[ ] exporting struct and fields
|
||||
[ ] funcs: constructor pattern
|
||||
[ ] using anonymous structs when testing
|
||||
[ ] pointers:
|
||||
[ ] struct tags {json encode/decode} - project?
|
||||
[ ] structs and pointers - later
|
||||
[ ] padding and memory layout - later
|
||||
[ ] empty struct (in channels section)
|
||||
[ ] using anonymous structs when testing
|
31
24-structs/xxx-wizards/marshal/main.go
Normal file
31
24-structs/xxx-wizards/marshal/main.go
Normal file
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Wizard is one of the greatest of people
|
||||
type Wizard struct {
|
||||
// name won't be marshalled (should be exported)
|
||||
Name string `json:name`
|
||||
Lastname string `json:"-"`
|
||||
Nick string `json:"nick"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
wizards := []Wizard{
|
||||
{Name: "Albert", Lastname: "Einstein", Nick: "emc2"},
|
||||
{Name: "Isaac", Lastname: "Newton", Nick: "apple"},
|
||||
{Name: "Stephen", Lastname: "Hawking", Nick: "blackhole"},
|
||||
{Name: "Marie", Lastname: "Curie", Nick: "radium"},
|
||||
{Name: "Charles", Lastname: "Darwin", Nick: "fittest"},
|
||||
}
|
||||
|
||||
bytes, err := json.Marshal(wizards)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Print(string(bytes))
|
||||
}
|
@ -5,17 +5,23 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"github.com/inancgumus/learngo/x-tba/structs/xxx-json/wizards"
|
||||
)
|
||||
|
||||
// Wizard is one of the greatest of people
|
||||
type Wizard struct {
|
||||
// name won't be marshalled (should be exported)
|
||||
Name string `json:name`
|
||||
Lastname string `json:"-"`
|
||||
Nick string `json:"nick"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
file, err := ioutil.ReadFile("../marshal/wizards.json")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
wizards := make([]wizards.Wizard, 10)
|
||||
wizards := make([]Wizard, 10)
|
||||
if json.Unmarshal(file, &wizards) != nil {
|
||||
panic(err)
|
||||
}
|
@ -1 +0,0 @@
|
||||
This section is in progress. I'm working hard to update the course all the time. Hold on!
|
@ -1,19 +0,0 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
type book struct {
|
||||
name, isbn string
|
||||
}
|
||||
|
||||
kafka := book{"Kafka's Revenge", "S-001"}
|
||||
golden := book{"Stay Golden", "S-002"}
|
||||
|
||||
books := make(map[book]int, 2)
|
||||
books[kafka] = 100
|
||||
books[golden] = 50
|
||||
|
||||
fmt.Printf("%s sold %d times\n", kafka.name, books[kafka])
|
||||
fmt.Printf("%s sold %d times\n", golden.name, books[golden])
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/inancgumus/learngo/x-tba/structs/xxx-json/wizards"
|
||||
)
|
||||
|
||||
func main() {
|
||||
wizards := []wizards.Wizard{
|
||||
{Name: "Albert", Lastname: "Einstein", Nick: "emc2"},
|
||||
{Name: "Isaac", Lastname: "Newton", Nick: "apple"},
|
||||
{Name: "Stephen", Lastname: "Hawking", Nick: "blackhole"},
|
||||
{Name: "Marie", Lastname: "Curie", Nick: "radium"},
|
||||
{Name: "Charles", Lastname: "Darwin", Nick: "fittest"},
|
||||
}
|
||||
|
||||
bytes, err := json.Marshal(wizards)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
ioutil.WriteFile("wizards.json", bytes, 0644)
|
||||
|
||||
//
|
||||
// PREVIOUSLY
|
||||
//
|
||||
|
||||
// names := [...][3]string{
|
||||
// {"First Name", "Last Name", "Nickname"},
|
||||
// {"Albert", "Einstein", "emc2"},
|
||||
// {"Isaac", "Newton", "apple"},
|
||||
// {"Stephen", "Hawking", "blackhole"},
|
||||
// {"Marie", "Curie", "radium"},
|
||||
// {"Charles", "Darwin", "fittest"},
|
||||
// }
|
||||
|
||||
// for i := range names {
|
||||
// n := names[i]
|
||||
// fmt.Printf("%-15s %-15s %-15s\n", n[0], n[1], n[2])
|
||||
|
||||
// if i == 0 {
|
||||
// fmt.Println(strings.Repeat("=", 50))
|
||||
// }
|
||||
// }
|
||||
}
|
@ -1 +0,0 @@
|
||||
[{"Name":"Albert","nick":"emc2"},{"Name":"Isaac","nick":"apple"},{"Name":"Stephen","nick":"blackhole"},{"Name":"Marie","nick":"radium"},{"Name":"Charles","nick":"fittest"}]
|
@ -1,9 +0,0 @@
|
||||
package wizards
|
||||
|
||||
// Wizard is one of the greatest of people
|
||||
type Wizard struct {
|
||||
// name won't be marshalled (should be exported)
|
||||
Name string `json:name`
|
||||
Lastname string `json:"-"`
|
||||
Nick string `json:"nick"`
|
||||
}
|
Reference in New Issue
Block a user