Last active
April 27, 2018 05:15
-
-
Save sekky0905/2b6a96e909adf5a0f443ba60c7d62c57 to your computer and use it in GitHub Desktop.
Goで学ぶポインタとアドレス ref: https://qiita.com/Sekky0905/items/d700cbc4399f0f6ec067
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
name := "太郎" |
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" | |
// Person は人間を表す構造体。 | |
type Person struct { | |
Name string | |
Age int | |
} | |
func main() { | |
// ポインタ型の変数を宣言する | |
// pがポインタ型変数 | |
// *Personポインタ型 | |
var p *Person | |
p = &Person{ | |
Name: "太郎", | |
Age: 20, | |
} | |
fmt.Printf("変数pに格納されているアドレス :%p", p) | |
} |
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
name :太郎 | |
namePoint :0x1040c128 | |
namePoint :太郎 | |
*namePointに二郎を代入後の*namePoint :二郎 | |
*namePointに二郎を代入後のnamePoint :0x1040c128 | |
*namePointに二郎を代入後のname :二郎 |
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
変数pに格納されているアドレス :0x1040a0d0 |
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" | |
// Person は人間を表す構造体。 | |
type Person struct { | |
Name string | |
Age int | |
} | |
func main() { | |
// ポインタ型の変数を宣言する | |
// pがポインタ型変数 | |
// *Personポインタ型 | |
var p *Person | |
p = &Person{ | |
Name: "太郎", | |
Age: 20, | |
} | |
fmt.Printf("p :%+v\n", p) | |
fmt.Printf("変数pに格納されているアドレス :%p", p) | |
} | |
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
p :&{Name:太郎 Age:20} | |
変数pに格納されているアドレス :0x1040a0d0 |
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" | |
// Person は人間を表す構造体。 | |
type Person struct { | |
Name string | |
Age int | |
} | |
func main() { | |
// 値として、pに代入 | |
p := Person{ | |
Name: "太郎", | |
Age: 20, | |
} | |
fmt.Printf("最初のp :%+v\n", p) | |
p2 := p | |
p2.Name = "二郎" | |
p2.Age = 21 | |
fmt.Printf("p2で二郎に書き換えを行なったはずのp :%+v\n", p) | |
// &pで*Person(Personのポインタ型)を生成する | |
// p3はpのアドレスが格納されている状態になる | |
p3 := &p | |
p3.Name = "二郎" | |
p3.Age = 21 | |
fmt.Printf("p3で二郎に書き換えを行なったp :%+v\n", p) | |
} | |
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
最初のp :{Name:太郎 Age:20} | |
p2で二郎に書き換えを行なったはずのp :{Name:太郎 Age:20} | |
p3で二郎に書き換えを行なったp :{Name:二郎 Age:21} |
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" | |
func main() { | |
name := "太郎" | |
fmt.Printf("name :%v\n", name) | |
namePoint := &name | |
// namePointは、&nameが格納されているだけなので、stringへのポインタである *string型の値が格納されている。 | |
fmt.Printf("namePoint :%v\n", namePoint) | |
// namePointが指している変数は、"*namePoint"という感じで、"*"をつけて表す。 | |
fmt.Printf("namePoint :%v\n", *namePoint) | |
} | |
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
name :太郎 | |
namePoint :0x1040c128 | |
namePoint :太郎 |
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" | |
func main() { | |
name := "太郎" | |
fmt.Printf("name :%v\n", name) | |
namePoint := &name | |
// namePointは、&nameが格納されているだけなので、stringへのポインタである*string型の値が格納されている。 | |
fmt.Printf("namePoint :%v\n", namePoint) | |
// namePointが指している変数は、"*namePoint"という感じで、"*"をつけて表す。 | |
fmt.Printf("namePoint :%v\n", *namePoint) | |
*namePoint = "二郎" | |
// *namePointに値を代入することもできる。 | |
fmt.Printf("*namePointに二郎を代入後の*namePoint :%v\n", *namePoint) | |
// 再代入したところで、namePointに格納されている*string型の値(アドレス)自体は、変わらない | |
fmt.Printf("*namePointに二郎を代入後のnamePoint :%v\n", namePoint) | |
// stringへのポインタである*string型の値(nameに格納されている値)を書き換えたので、nameの値も変更される。 | |
fmt.Printf("*namePointに二郎を代入後のname :%v\n", name) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment