1
1

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.

「A Tour of Go」でGo言語を学ぶ(~#28)

Posted at

今回は構造体について。今回はコードが2つ。感想は最後にまとめて書く。
#24は型の紹介だったので飛ばす。

struct.go
package main

import "fmt"

type Vertex struct {
	X int
	Y int
}

func main() {
	v := Vertex{1, 2}
	v.X = 4
	fmt.Println(v.X)
}

結果


4

次、ポインタとか。

struct2.go
package main

import "fmt"

type Vertex struct {
	X, Y int
}

var (
	p = Vertex{1, 2}  // has type Vertex
	q = &Vertex{1, 2} // has type *Vertex
	r = Vertex{X: 1}  // Y:0 is implicit
	s = Vertex{}      // X:0 and Y:0
)

func main() {
	fmt.Println(p, q, r, s)
}

結果


{1 2} &{1 2} {1 0} {0 0}

感想


まず1つ目のやつ。
C言語とかで見たことのあるような感じ。typeキーワードはC言語のtypedefと多分同じ。

2つ目。
Goでもポインタは使えるけど、俺は使うかわからない。実際に使うと、あとで見返した時に忘れそう。var (~~)はまとめて初期化出来るようにするためかな。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?