2018年8月23日木曜日

開発環境

  • macOS High Sierra - Apple (OS)
  • Emacs (Text Editor)
  • Go (プログラミング言語)

Introducing Go: Build Reliable, Scalable Programs (Caleb Doxsey (著)、O'Reilly Media)のChapter 7.(Structs and Interfaces)、Exercises(No. 1315)1.を取り組んでみる。

コード(Emacs)

package main

import "fmt"

type MyInt struct {
 x int
}

// method
func (i *MyInt) double() int {
 return 2 * i.x
}
func (i MyInt) double1() int {
 return 2 * i.x
}

// functions
func double(i *MyInt) int {
 return 2 * i.x
}
func double1(i MyInt) int {
 return 2 * i.x
}
func main() {
 a := MyInt{x: 10}
 fmt.Println((&a).x, a.x)

 b := MyInt{20}
 var c MyInt
 c.x = 30
 xs := []MyInt{a, b, c}
 for _, t := range xs {
  fmt.Println(t, t.double(), t.double1(), double(&t), double1(t))
 }
}

入出力結果(Terminal)

$ go run sample1.go
10 10
{10} 20 20 20 20
{20} 40 40 40 40
{30} 60 60 60 60
$

0 コメント:

コメントを投稿