「Go言語」構造体(struct)を使用するサンプル

書式
type 構造体名 struct {
メンバ変数
}
使用例

package main

import "fmt"

// 構造体(struct) Booksの定義
type Books struct {
   title string
   author string
   subject string
   book_id int
}


func main() {

    // 構造体(struct)の各メンバー構成
    fmt.Println(Books{"Go言語", "www.arkgame.com", "Go言語入門", 678})

    // key => value 形式
    fmt.Println(Books{title: "Go言語", author: "www.arkgame.com", subject: "Go言語入門", book_id: 678})

    // subject とbook_id 略
   fmt.Println(Books{title: "Go言語", author: "www.arkgame.com"})
}

実行結果
>go run sample.go
{Go言語 www.arkgame.com Go言語入門 678}
{Go言語 www.arkgame.com Go言語入門 678}
{Go言語 www.arkgame.com 0}

Go言語

Posted by arkgame