Guest User

Untitled

a guest
Oct 16th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. // テスト用構造体
  6. type example struct {
  7. Name string
  8. }
  9.  
  10. // example構造体を格納する配列の型
  11. type examples []*example
  12.  
  13. // Example exampleのNameフィールドに引数のstringを指定して返す
  14. func doExample(n string) (r *example) {
  15. r = new(example)
  16. r.Name = n
  17. return r
  18. }
  19.  
  20. func main() {
  21. // example構造体配列を宣言
  22. var exs examples
  23.  
  24. // 配列に構造体を格納
  25. exs = append(exs, doExample("A"))
  26. exs = append(exs, doExample("B"))
  27.  
  28. // 格納した構造体のNameフィールドを出力
  29. for _, i := range exs {
  30. fmt.Println(i.Name)
  31. }
  32. }
Add Comment
Please, Sign In to add comment