2020年6月3日水曜日

開発環境

Go Systems Programming: Master Linux and Unix system level programming with Go (Mihalis Tsoukalos(著)、Packt Publishing)のChapter 2(Writing Programs in Go)、Exercises 10.の解答を求めてみる。

コード

package main

import "fmt"

type coordinate int
type coordinates interface {
 xaxis() coordinate
 yaxis() coordinate
}
type point2D struct {
 x, y coordinate
}

func (p point2D) xaxis() coordinate {
 return p.x
}
func (p point2D) yaxis() coordinate {
 return p.y
}
func findCoordinate(p coordinates) {
 fmt.Printf("x: %v, y: %v\n", p.xaxis(), p.yaxis())
}

type coordinates3d interface {
 xaxis() coordinate
 yaxis() coordinate
 zaxis() coordinate
}

type point3D struct {
 x, y, z coordinate
}

func (p point3D) xaxis() coordinate {
 return p.x
}
func (p point3D) yaxis() coordinate {
 return p.y
}
func (p point3D) zaxis() coordinate {
 return p.z
}
func findCoordinates3D(p coordinates3d) {
 fmt.Printf("x: %v, y: %v, z: %v\n", p.xaxis(), p.yaxis(), p.zaxis())
}
func main() {
 p := point3D{x: 1, y: 2, z: 3}
 findCoordinates3D(p)
 findCoordinate(p)
}

入出力結果(Zsh、PowerShell、Terminal)

% go run point.go
x: 1, y: 2, z: 3
x: 1, y: 2
%

0 コメント:

コメントを投稿