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 1 year has passed since last update.

[Go] Range

Posted at

Range

Range は要素の値のコピーが返されるので、
返される要素を変更しても、元の値を影響しない
変更したい場合は、直接参照すれば

サンプルコード

package main

import "fmt"

func main() {
	users := [3]string{"A", "B", "C"}
	fmt.Printf("%#v\n", users)

	for _, u := range users {
		u += "edited"
	}
	fmt.Printf("%#v\n", users)

	for i := 0; i < len(users); i++ {
		users[i] += " edited"
	}
	fmt.Printf("%#v\n", users)
}
output
[3]string{"A", "B", "C"}
[3]string{"A", "B", "C"}
[3]string{"A edited", "B edited", "C edited"}
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?