LoginSignup
1
0

More than 1 year has passed since last update.

【Golang】ポインタ型の int 変数に値をセットする

Last updated at Posted at 2021-11-24

ここ 1 年で作成・更新された記事を「"golang" ポインタに値をセットする int」でググっても、わかりやすいタイトルがヒットしなかったので、自分のググラビリティとして。

TL; DR (今北産業)

変数の前に*を付ける
func resetToTen(i *int) {
    *i = 10 // <- ここ
}
動くサンプル
package main

import "fmt"

func main() {
    i := 100

    resetToTen(&i)

    fmt.Println(i)
}

func resetToTen(i *int) {
    *i = 10
}
// Output: 10

参考文献

1
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
1
0