add: struct encoding/decoding
This commit is contained in:
		
							
								
								
									
										42
									
								
								24-structs/06-encoding/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								24-structs/06-encoding/main.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | |||||||
|  | // For more tutorials: https://blog.learngoprogramming.com | ||||||
|  | // | ||||||
|  | // Copyright © 2018 Inanc Gumus | ||||||
|  | // Learn Go Programming Course | ||||||
|  | // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ | ||||||
|  | // | ||||||
|  |  | ||||||
|  | package main | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"encoding/json" | ||||||
|  | 	"fmt" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | type permissions map[string]bool // #3 | ||||||
|  |  | ||||||
|  | type user struct { // #1 | ||||||
|  | 	Name        string      `json:"username"` | ||||||
|  | 	Password    string      `json:"-"` | ||||||
|  | 	Permissions permissions `json:"perms,omitempty"` // #6 | ||||||
|  |  | ||||||
|  | 	// name        string // #1 | ||||||
|  | 	// password    string // #1 | ||||||
|  | 	// permissions // #3 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func main() { | ||||||
|  | 	users := []user{ // #2 | ||||||
|  | 		{"inanc", "1234", nil}, | ||||||
|  | 		{"god", "42", permissions{"admin": true}}, | ||||||
|  | 		{"devil", "66", permissions{"write": true}}, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// out, err := json.Marshal(users) // #4 | ||||||
|  | 	out, err := json.MarshalIndent(users, "", "\t") // #5 | ||||||
|  | 	if err != nil {                                 // #4 | ||||||
|  | 		fmt.Println(err) | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	fmt.Println(string(out)) // #4 | ||||||
|  | } | ||||||
							
								
								
									
										47
									
								
								24-structs/07-decoding/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								24-structs/07-decoding/main.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | |||||||
|  | // For more tutorials: https://blog.learngoprogramming.com | ||||||
|  | // | ||||||
|  | // Copyright © 2018 Inanc Gumus | ||||||
|  | // Learn Go Programming Course | ||||||
|  | // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ | ||||||
|  | // | ||||||
|  |  | ||||||
|  | package main | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"bufio" | ||||||
|  | 	"encoding/json" | ||||||
|  | 	"fmt" | ||||||
|  | 	"os" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | type user struct { | ||||||
|  | 	Name        string          `json:"username"` | ||||||
|  | 	Permissions map[string]bool `json:"perms"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func main() { | ||||||
|  | 	var input []byte | ||||||
|  | 	for in := bufio.NewScanner(os.Stdin); in.Scan(); { | ||||||
|  | 		input = append(input, in.Bytes()...) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	var users []user | ||||||
|  | 	if err := json.Unmarshal(input, &users); err != nil { | ||||||
|  | 		fmt.Println(err) | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	for _, user := range users { | ||||||
|  | 		fmt.Print("+ ", user.Name) | ||||||
|  |  | ||||||
|  | 		switch p := user.Permissions; { | ||||||
|  | 		case p == nil: | ||||||
|  | 			fmt.Print(" has no power.") | ||||||
|  | 		case p["admin"]: | ||||||
|  | 			fmt.Print(" is an admin.") | ||||||
|  | 		case p["write"]: | ||||||
|  | 			fmt.Print(" can write.") | ||||||
|  | 		} | ||||||
|  | 		fmt.Println() | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										17
									
								
								24-structs/07-decoding/users.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								24-structs/07-decoding/users.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | |||||||
|  | [ | ||||||
|  | 	{ | ||||||
|  | 		"username": "inanc" | ||||||
|  | 	}, | ||||||
|  | 	{ | ||||||
|  | 		"username": "god", | ||||||
|  | 		"perms": { | ||||||
|  | 			"admin": true | ||||||
|  | 		} | ||||||
|  | 	}, | ||||||
|  | 	{ | ||||||
|  | 		"username": "devil", | ||||||
|  | 		"perms": { | ||||||
|  | 			"write": true | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | ] | ||||||
		Reference in New Issue
	
	Block a user