-
Notifications
You must be signed in to change notification settings - Fork 13
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
Showing
5 changed files
with
170 additions
and
0 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,46 @@ | ||
package examples | ||
|
||
import "encoding/json" | ||
|
||
type Person struct { | ||
Name string `json:"name"` | ||
Family string `json:"family"` | ||
Age int `json:"age,omitempty"` | ||
} | ||
|
||
func MarshalExample1() { | ||
person1 := Person{Name: "Ali", Family: "Rezaee", Age: 17} | ||
person2 := Person{Name: "Milad", Family: "Mohammadi", Age: 20} | ||
person3 := Person{Name: "Peyman", Family: "Mohammadi", Age: 0} | ||
|
||
person1Json, err := json.Marshal(person1) | ||
if err != nil { | ||
panic(err) | ||
} | ||
person2Json, err := json.Marshal(person2) | ||
if err != nil { | ||
panic(err) | ||
} | ||
person3Json, err := json.Marshal(person3) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
println(string(person1Json)) | ||
println(string(person2Json)) | ||
println(string(person3Json)) | ||
} | ||
|
||
func MarshalExample2() { | ||
person1 := Person{Name: "Ali", Family: "Rezaee", Age: 17} | ||
person2 := Person{Name: "Milad", Family: "Mohammadi", Age: 20} | ||
person3 := Person{Name: "Peyman", Family: "Mohammadi", Age: 0} | ||
|
||
persons := []Person{person1, person2, person3} | ||
personsJson, err := json.Marshal(persons) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
println(string(personsJson)) | ||
} |
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,34 @@ | ||
package examples | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
func UnmarshalExample1() { | ||
person1Json := []byte(`{"name":"Ali","family":"Rezaee","age":17}`) | ||
|
||
var person1 = Person{} | ||
err := json.Unmarshal(person1Json, &person1) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
println(person1.Name) | ||
println(person1.Family) | ||
println(person1.Age) | ||
} | ||
|
||
func UnmarshalExample2() { | ||
personsJson := []byte(`[{"name":"Ali","family":"Rezaee","age":17},{"name":"Milad","family":"Mohammadi","age":20},{"name":"Peyman","family":"Mohammadi"}]`) | ||
|
||
var persons = []Person{} | ||
err := json.Unmarshal(personsJson, &persons) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Printf("%+v", persons) | ||
} |
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,76 @@ | ||
package examples | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type BookingLocation struct { | ||
Country string `json:"country"` | ||
State string `json:"state"` | ||
StateName string `json:"stateName"` | ||
ZipCode string `json:"zipcode"` | ||
Timezone string `json:"timezone"` | ||
Latitude string `json:"latitude"` | ||
Longitude string `json:"longitude"` | ||
City string `json:"city"` | ||
Continent string `json:"continent"` | ||
} | ||
|
||
func UnmarshalExample3() { | ||
response := make(chan []byte) | ||
go GetResponse(response, "https://geolocation.onetrust.com/cookieconsentpub/v1/geo/location") | ||
|
||
var location = BookingLocation{} | ||
err := json.Unmarshal(<-response, &location) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
println(location.City) | ||
println(location.Country) | ||
println(location.StateName) | ||
} | ||
|
||
func GetResponse(content chan<- []byte, url string) { | ||
|
||
client := http.Client{} | ||
|
||
request, err := http.NewRequest("GET", url, nil) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
request.Header = http.Header{} | ||
|
||
request.Header.Add("accept", "application/json") | ||
|
||
response, err := client.Do(request) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
defer response.Body.Close() | ||
|
||
responseBody, err := ioutil.ReadAll(response.Body) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
PrintlnWithTime(string(responseBody)) | ||
PrintlnWithTime("Before set content") | ||
content <- responseBody | ||
PrintlnWithTime("After set content") | ||
|
||
} | ||
|
||
func PrintlnWithTime(args ...any) { | ||
fmt.Printf("Time: %s, %v\n", time.Now().Format(time.RFC3339Nano), args) | ||
} |
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,3 @@ | ||
module jsonExamples | ||
|
||
go 1.18 |
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,11 @@ | ||
package main | ||
|
||
import "jsonExamples/examples" | ||
|
||
func main() { | ||
// examples.MarshalExample1() | ||
// examples.MarshalExample2() | ||
// examples.UnmarshalExample1() | ||
// examples.UnmarshalExample2() | ||
examples.UnmarshalExample3() | ||
} |