0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Go】ポインタの基本

0
Posted at

こちらの記事を参考に、Goにおけるポインタらへんについて雑にまとめます。

package main

import "fmt"

//変数nameはstring型と宣言
var name string

func main() {

	name = "aoki"
	//変数name_addressはnameのポインタ型と暗黙的に宣言
	name_address := &name
	fmt.Printf("%+v\n", name_address) //ポインタ変数を表示
	fmt.Printf("%+v\n", *name_address) //そのアドレスに入っている値を表示
}

実行結果

0x117a250
aoki
package main

import "fmt"

//変数nameはstring型と宣言
var name string

func main() {
	name = "aoki"
	name2 := name
	name2 = "tanaka"
	fmt.Printf("%+v\n", name2)
	fmt.Printf("%+v\n", name) //nameの値は変わらない

	name3 := &name
	*name3 = "nishimura"
	fmt.Printf("%+v\n", name3)
	fmt.Printf("%+v\n", *name3)
	fmt.Printf("%+v\n", name) //同じアドレスなので、nameの値は変わる
}

実行結果

tanaka
aoki
0x117a250
nishimura
nishimura
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?