「Go言語」構造体(struct)を使うサンプル
書式
type 構造体名 struct {
name string
age int
}
使用例
package main
import "fmt"
//構造体(struct)の定義
type Books struct {
title string
author string
subject string
book_id int
}
// main関数
func main() {
// 構造体(struct)作成
fmt.Println(Books{"Go言語", "www.arkgame.com", "Go言語入門", 98765438})
// key => value 形式
fmt.Println(Books{title: "Go言語", author: "www.arkgame.com", subject: "Go言語入門", book_id: 98765438})
// 0または空白
fmt.Println(Books{title: "Go言語", author: "www.arkgame.com"})
}
package main
import "fmt"
//構造体(struct)の定義
type Books struct {
title string
author string
subject string
book_id int
}
// main関数
func main() {
// 構造体(struct)作成
fmt.Println(Books{"Go言語", "www.arkgame.com", "Go言語入門", 98765438})
// key => value 形式
fmt.Println(Books{title: "Go言語", author: "www.arkgame.com", subject: "Go言語入門", book_id: 98765438})
// 0または空白
fmt.Println(Books{title: "Go言語", author: "www.arkgame.com"})
}
package main import "fmt" //構造体(struct)の定義 type Books struct { title string author string subject string book_id int } // main関数 func main() { // 構造体(struct)作成 fmt.Println(Books{"Go言語", "www.arkgame.com", "Go言語入門", 98765438}) // key => value 形式 fmt.Println(Books{title: "Go言語", author: "www.arkgame.com", subject: "Go言語入門", book_id: 98765438}) // 0または空白 fmt.Println(Books{title: "Go言語", author: "www.arkgame.com"}) }
実行結果
>go run test.go
{Go言語 www.arkgame.com Go言語入門 98765438}
{Go言語 www.arkgame.com Go言語入門 98765438}
{Go言語 www.arkgame.com 0}