forked from GoesToEleven/GolangTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d07c985
commit 307974c
Showing
29 changed files
with
186 additions
and
206 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type person struct { | ||
first string | ||
last string | ||
age int | ||
} | ||
|
||
func (p *person) fullName() string { | ||
fmt.Printf("Inside method: %p\n", &p) | ||
return p.first + p.last | ||
} | ||
|
||
func main() { | ||
p1 := person{"James", "Bond", 20} | ||
p2 := &person{"Miss", "Moneypenny", 18} | ||
|
||
// address in main is different from address in method | ||
fmt.Println(p1.fullName()) | ||
fmt.Printf("Inside main: %p\n", &p1) | ||
|
||
// method works for either a value or pointer | ||
fmt.Println(p2.fullName()) | ||
} | ||
|
||
// p1 is the receiver value for the call to fullName | ||
// fullName is operating on its own value of p1 |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions
21
20_struct/08_marshal_unmarshal/01_marshal/01_exported/main.go
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type Person struct { | ||
First string | ||
Last string | ||
Age int | ||
notExported int | ||
} | ||
|
||
func main() { | ||
p1 := Person{"James", "Bond", 20, 007} | ||
bs, _ := json.Marshal(p1) | ||
fmt.Println(bs) | ||
fmt.Printf("%T \n", bs) | ||
fmt.Println(string(bs)) | ||
} |
19 changes: 19 additions & 0 deletions
19
20_struct/08_marshal_unmarshal/01_marshal/02_unexported/main.go
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type Person struct { | ||
first string | ||
last string | ||
age int | ||
} | ||
|
||
func main() { | ||
p1 := Person{"James", "Bond", 20} | ||
fmt.Println(p1) | ||
bs, _ := json.Marshal(p1) | ||
fmt.Println(string(bs)) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type Person struct { | ||
First string | ||
Last string `json:"-"` | ||
Age int `json:"wisdom score"` | ||
} | ||
|
||
func main() { | ||
p1 := Person{"James", "Bond", 20} | ||
bs, _ := json.Marshal(p1) | ||
fmt.Println(string(bs)) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type Person struct { | ||
First string | ||
Last string | ||
Age int | ||
} | ||
|
||
func main() { | ||
var p1 Person | ||
fmt.Println(p1.First) | ||
fmt.Println(p1.Last) | ||
fmt.Println(p1.Age) | ||
|
||
bs := []byte(`{"First":"James", "Last":"Bond", "Age":20}`) | ||
json.Unmarshal(bs, &p1) | ||
|
||
|
||
fmt.Println("--------------") | ||
fmt.Println(p1.First) | ||
fmt.Println(p1.Last) | ||
fmt.Println(p1.Age) | ||
fmt.Printf("%T \n", p1) | ||
} |
28 changes: 28 additions & 0 deletions
28
20_struct/08_marshal_unmarshal/02_unmarshal/02_tags/main.go
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type Person struct { | ||
First string | ||
Last string | ||
Age int `json:"wisdom score"` | ||
} | ||
|
||
func main() { | ||
var p1 Person | ||
fmt.Println(p1.First) | ||
fmt.Println(p1.Last) | ||
fmt.Println(p1.Age) | ||
|
||
bs := []byte(`{"First":"James", "Last":"Bond", "wisdom score":20}`) | ||
json.Unmarshal(bs, &p1) | ||
|
||
fmt.Println("--------------") | ||
fmt.Println(p1.First) | ||
fmt.Println(p1.Last) | ||
fmt.Println(p1.Age) | ||
fmt.Printf("%T \n", p1) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
) | ||
|
||
type Person struct { | ||
First string | ||
Last string | ||
Age int | ||
notExported int | ||
} | ||
|
||
func main() { | ||
p1 := Person{"James", "Bond", 20, 007} | ||
json.NewEncoder(os.Stdout).Encode(p1) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
"fmt" | ||
) | ||
|
||
type Person struct { | ||
First string | ||
Last string | ||
Age int | ||
notExported int | ||
} | ||
|
||
func main() { | ||
var p1 Person | ||
str := strings.NewReader(`{"First":"James", "Last":"Bond", "Age":20}`) | ||
json.NewDecoder(str).Decode(&p1) | ||
|
||
fmt.Println(p1.First) | ||
fmt.Println(p1.Last) | ||
fmt.Println(p1.Age) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.