2018年8月22日水曜日

開発環境

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

Introducing Go: Build Reliable, Scalable Programs (Caleb Doxsey (著)、O'Reilly Media)のChapter 6.(Functions)、Exercises(No. 1105)10、11.を取り組んでみる。

コード(Emacs)

package main

import "fmt"

func square(x *float64) {
 *x = *x * *x
}

// 一時変数を使った分かりやすい方法
func swap(x *int, y *int) {
 t := *x
 *x = *y
 *y = t
}

// 一時変数を使わない方法
func swap1(x *int, y *int) {
 *x += *y
 *y = *x - *y
 *x -= *y
}
func main() {
 fmt.Println("10.")
 x := 1.5
 square(&x)
 fmt.Println(2.25 == x)

 fmt.Println("11.")
 a := 1
 b := 2
 swap(&a, &b)
 fmt.Println(a, b)
 swap1(&a, &b)
 fmt.Println(a, b)
}

入出力結果(Terminal)

$ go run sample9.go
10.
true
11.
2 1
1 2
$

0 コメント:

コメントを投稿