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 3 years have passed since last update.

【Go言語】構造体(struct)について

Posted at

#構造体等とは何か
色々あるデータを1つにまとめたもの。
Go言語にはクラスというものが存在しないが、構造体を使用することでクラスのような振る舞いをすることができる。

#定義方法

main.go
package main

func main() {
	type people struct {
		name   string
		age    int
		gender string
	}
}
}

# 初期化方法

main.go
package main

func main() {
	type people struct {
		name   string
		age    int
		gender string
	}

	//people型で変数を定義
	var people1 people

	//それぞれに値を代入
	people1.name = " yamada"
	people1.age = 21
	people1.gender = "man"

     //出力
    fmt.Println(people1.name) //yamada
	fmt.Println(people1.age) //21
	fmt.Println(people1.gender) //man
}

上記のように構造体を使うとクラスっぽい振る舞いができます。

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?