「Go言語」構造体(struct)のメンバー変数をアクセスするサンプル

書式
type 構造体名 struct {
name string
age int
}
使用例

package main

import "fmt"

//構造体Booksの定義
type Books struct {
   //メンバ
   title string
   author string
   subject string
   book_id int
}

func main() {
   var Book1 Books        /* Books構造体  Book1 */
   var Book2 Books        /* Books構造体 Book2*/

   /* Book1のメンバー変数値設定*/
   Book1.title = "Go 言語"
   Book1.author = "www.arkgame.com"
   Book1.subject = "Go 言語入門"
   Book1.book_id = 6688

   /* Book2のメンバー変数値設定*/
   Book2.title = "Python 教程"
   Book2.author = "www.arkgame.com"
   Book2.subject = "Python 言語入門"
   Book2.book_id = 7799

   /*  Book1のメンバー変数値出力 */
   fmt.Printf( "Book 1 title : %s\n", Book1.title)
   fmt.Printf( "Book 1 author : %s\n", Book1.author)
   fmt.Printf( "Book 1 subject : %s\n", Book1.subject)
   fmt.Printf( "Book 1 book_id : %d\n", Book1.book_id)

   /* Book2のメンバー変数値出力 */
   fmt.Printf( "Book 2 title : %s\n", Book2.title)
   fmt.Printf( "Book 2 author : %s\n", Book2.author)
   fmt.Printf( "Book 2 subject : %s\n", Book2.subject)
   fmt.Printf( "Book 2 book_id : %d\n", Book2.book_id)
}

実行結果
>go run test.go
Book 1 title : Go 言語
Book 1 author : www.arkgame.com
Book 1 subject : Go 言語入門
Book 1 book_id : 6688
Book 2 title : Python 教程
Book 2 author : www.arkgame.com
Book 2 subject : Python 言語入門
Book 2 book_id : 7799

Go言語

Posted by arkgame