「Go言語」構造体のメンバ変数を利用するサンプル

書式
var 変数名 構造体名
変数名.メンバー変数名
使用例

package main

import "fmt"
//構造体の定義
type Books struct {
   title string
   author string
   subject string
   book_id int
}

func main() {
   var cftA Books /*構造体変数cftAの定義*/  
   var cftB Books  /*構造体変数cftBの定義*/

   /* cftA変数のフィールドを設定 */
   cftA.title = "Java入門"
   cftA.author = "www.arkgame.com"
   cftA.subject = "Java 基本知識"
   cftA.book_id = 6123

   /* cftB変数のフィールドを設定 */
   cftB.title = "Python入門"
   cftB.author = "www.arkgame.com"
   cftB.subject = "Python 基本知識"
   cftB.book_id = 6456

   /* cftA情報を出力 */
   fmt.Println("***cftAフィールドの値を出力する結果:***")
   fmt.Printf( "cftA タイトル : %s\n", cftA.title)
   fmt.Printf( "cftA 著者 : %s\n", cftA.author)
   fmt.Printf( "cftA 件名: %s\n", cftA.subject)
   fmt.Printf( "cftA 番号: %d\n", cftA.book_id)

   /* cftB情報を出力 */
   fmt.Println("***cftBフィールドの値を出力する結果:***")
   fmt.Printf( "cftB タイトル : %s\n", cftB.title)
   fmt.Printf( "cftB 著者 : %s\n", cftB.author)
   fmt.Printf( "cftB 件名 : %s\n", cftB.subject)
   fmt.Printf( "cftB 番号 : %d\n", cftB.book_id)
}

実行結果
>go run sample.go
***cftAフィールドの値を出力する結果:***
cftA タイトル : Java入門
cftA 著者 : www.arkgame.com
cftA 件名: Java 基本知識
cftA 番号: 6123
***cftBフィールドの値を出力する結果:***
cftB タイトル : Python入門
cftB 著者 : www.arkgame.com
cftB 件名 : Python 基本知識
cftB 番号 : 6456

Go言語

Posted by arkgame