Created
December 5, 2016 18:42
-
-
Save kmtr/ea423d2129c18d9db909a51ba89da048 to your computer and use it in GitHub Desktop.
Goの構造体の研究 ref: http://qiita.com/kmtr/items/1e7caf92aa8bb587906d
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
type Account struct { | |
Email string | |
Password string | |
Rank int | |
} |
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
func main() { | |
account := new(Account) | |
account.Email = "sample@example.com" | |
account.Password = "password" | |
account.Rank = 1 | |
fmt.Printf("%v", account) | |
} |
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
type AccountParam struct { | |
Account | |
PasswordCurrent string `json:"password_current"` | |
Rank int `json:"user_rank"` | |
} |
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
package main | |
type Account struct { | |
Email string `json:"email"` | |
Password string `json:"password"` | |
Rank int `json:"rank"` | |
} | |
type AccountParam struct { | |
Account | |
PasswordCurrent string `json:"password_current"` | |
} | |
func main() { | |
var a *Account | |
a = new(AccountParam) | |
} |
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
func main() { | |
a := AccountParam{} | |
b := Account{} | |
a.Account = b | |
fmt.Println(a) | |
} |
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
func main() { | |
account := &Account{ | |
Email: "sample@example.com", | |
Password: "password", | |
Rank: 1, | |
} | |
fmt.Printf("%v", account) | |
} |
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
func main() { | |
account := Account{ | |
Email: "sample@example.com", | |
Password: "password", | |
Rank: 1, | |
} | |
fmt.Printf("%v", account) | |
} |
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
package main | |
import "fmt" | |
type Account struct { | |
Email string | |
Password string | |
Rank int | |
} | |
func updatePassword(account Account, password string) { | |
account.Password = password | |
fmt.Printf("account.Password(func) : %s\n", account.Password) | |
fmt.Printf("account.Password(pointer): %p\n", &account) | |
} | |
func updatePasswordP(account *Account, password string) { | |
account.Password = password | |
fmt.Printf("accountP.Password(func) : %s\n", account.Password) | |
fmt.Printf("accountP.Password(pointer): %p\n", account) | |
} | |
func main() { | |
account := Account{ | |
Email: "sample@example.com", | |
Password: "password", | |
Rank: 1, | |
} | |
fmt.Printf("account.Password(pointer): %p\n", &account) | |
fmt.Printf("account.Password(before): %s\n", account.Password) | |
updatePassword(account, "new password") | |
fmt.Printf("account.Password(after) : %s\n", account.Password) | |
fmt.Println("-----------------") | |
accountP := &Account{ | |
Email: "sample@example.com", | |
Password: "password", | |
Rank: 1, | |
} | |
fmt.Printf("accountP.Password(pointer): %p\n", accountP) | |
fmt.Printf("accountP.Password(before): %s\n", accountP.Password) | |
updatePasswordP(accountP, "new password") | |
fmt.Printf("accountP.Password(after) : %s\n", accountP.Password) | |
} |
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
$ go run ac.go | |
account.Password(pointer): 0xc420014150 | |
account.Password(before): password | |
account.Password(func) : new password | |
account.Password(pointer): 0xc420014180 | |
account.Password(after) : password | |
----------------- | |
accountP.Password(pointer): 0xc4200141b0 | |
accountP.Password(before): password | |
accountP.Password(func) : new password | |
accountP.Password(pointer): 0xc4200141b0 | |
accountP.Password(after) : new password |
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
{"Email":"sample@example.com","Password":"password","Rank":1} |
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
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
) | |
type Account struct { | |
Email string `json:"email"` | |
Password string `json:"password"` | |
Rank int `json:"rank"` | |
} | |
func main() { | |
account := &Account{ | |
Email: "sample@example.com", | |
Password: "password", | |
Rank: 1, | |
} | |
// encode | |
buf := new(bytes.Buffer) | |
json.NewEncoder(buf).Encode(account) | |
fmt.Println(buf.String()) | |
// decode | |
decoded := new(Account) | |
json.NewDecoder(buf).Decode(decoded) | |
fmt.Println(decoded) | |
} | |
// 実行結果 | |
// {"email":"sample@example.com","password":"password","rank":1} | |
// | |
// &{sample@example.com password 1} |
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
// package, import, Account構造体定義省略 | |
type UserAccount Account | |
func main() { | |
account := &UserAccount{ | |
Email: "sample@example.com", | |
Password: "password", | |
Rank: 1, | |
} | |
// encode | |
buf := new(bytes.Buffer) | |
json.NewEncoder(buf).Encode(account) | |
fmt.Println(buf.String()) | |
// decode | |
decoded := new(UserAccount) | |
json.NewDecoder(buf).Decode(decoded) | |
fmt.Println(decoded) | |
} | |
// 実行結果 | |
// {"email":"sample@example.com","password":"password","rank":1} | |
// | |
// &{sample@example.com password 1} |
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
// package, import, Account構造体定義省略 | |
type AccountParam struct { | |
Account | |
} | |
func main() { | |
account := new(AccountParam) | |
account.Email = "sample@example.com" | |
account.Password = "password" | |
account.Rank = | |
// encode | |
buf := new(bytes.Buffer) | |
json.NewEncoder(buf).Encode(account) | |
fmt.Println(buf.String()) | |
// decode | |
decoded := new(AccountParam) | |
json.NewDecoder(buf).Decode(decoded) | |
fmt.Println(decoded) | |
} | |
// 実行結果 | |
// {"email":"sample@example.com","password":"password","rank":1} | |
// | |
// &{sample@example.com password 1} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment