LoginSignup
18
14

More than 5 years have passed since last update.

Goの構造体を配列に入れてみたメモ

Last updated at Posted at 2017-10-16

Goの構造体を配列に入れた時の備忘録です。

package main

import "fmt"

// テスト用構造体
type example struct {
    Name string
}

// example構造体を格納する配列の型
type examples []*example

// doExample exampleのNameフィールドに引数のstringを指定して返す
func doExample(n string) (r *example) {
    r = new(example)
    r.Name = n
    return r
}

func main() {
    // example構造体配列を宣言
    var exs examples

    // 配列に構造体を格納
    exs = append(exs, doExample("A"))
    exs = append(exs, doExample("B"))

    // 格納した構造体のNameフィールドを出力
    for _, i := range exs {
        fmt.Println(i.Name)
    }
}
18
14
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
18
14