0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

A Tour of Go メモ 【4】二日目

Posted at

A Tour of Go

Range

var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}

func main() {
	for i, v := range pow {
		fmt.Printf("2**%d = %d\n", i, v)
	}
	for i, a := range hiragana {
	    fmt.Printf("「%s」は、%d番目\n", a, i+1)
	}
}
> 2**0 = 1
> 2**1 = 2
> 2**2 = 4
> 2**3 = 8
> 2**4 = 16
> 2**5 = 32
> 2**6 = 64
> 2**7 = 128
> 「あ」は、1番目
> 「い」は、2番目
> 「う」は、3番目
> 「え」は、4番目
> 「お」は、5番目

rangeと"_(アンダーバー)"

func main() {
	pow := make([]int, 10)
	for i := range pow {
		pow[i] = 1 << uint(i) // == 2**i
	}
	for _, value := range pow {
		fmt.Printf("%d\n", value)
	}
    s := "hello"
	for i, _ := range s {
	    fmt.Printf("%d回目の%s\n", i+1, s)
	}
}

> 1
> 2
> 4
> 8
> 16
> 32
> 64
> 128
> 256
> 512
> 1回目のhello
> 2回目のhello
> 3回目のhello
> 4回目のhello
> 5回目のhello

Maps

make(map["keyになる値の型"]"valueになる値の型")
例えば、
・ keyも値もint型
=> make(map[int]int)
.keyも値もstring型
=> make(map[string]string)

type Vertex struct {
	Lat, Long float64
}

var m map[string]Vertex
//変数を定義するときにキーと値を設定できる
var n = map[string]Vertex{
    "key1" : Vertex {
        10.0, 10.000,
    },
	"key2" : Vertex {
         20.0, 20.000,	    
	},
}
var z = map[string]Vertex{
    "key1" : {333.33, 44.444},
    "key2" : {88.888, 999.999},
}

func main() {
    fmt.Println(m)
    //エラー。 初期化していないので、追加できない。
    m["insert"] = Vertex{
       11.111, 22.222,
    
    }

    fmt.Println(n)
    // 初期化
	m = make(map[string]Vertex)
	fmt.Println(m)
	//キーの追加
	m["Bell Labs"] = Vertex{
		40.68433, -74.39967,
	}
	m["test"] = Vertex{
	    22222, 2020202,
	}
 	fmt.Println(m)
	fmt.Println(m["Bell Labs"])
}

> map[]
> map[key1:{10 10} key2:{20 20}]
> map[]
> map[Bell Labs:{40.68433 -74.39967} test:{22222 2.020202e+06}]
> {40.68433 -74.39967}

Mapの操作

func main() {
	m := make(map[string]int)

	m["Answer"] = 42
	fmt.Println(m)
    //keyを指定すると対応した要素(elem)を取得でkりう
	fmt.Println("The value:", m["Answer"])

	m["Answer"] = 48
	fmt.Println("The value:", m["Answer"])

    //要素の削除
	delete(m, "Answer")
	fmt.Println("The value:", m["Answer"])

    //keyに対応する値があるか
    //もしもkeyがあれば、対応する値がvに入る、keyがなければ、map作成時に指定した型のゼロ値が入る
    //okには、keyがあれば、true, なければfalseが入る
	v, ok := m["Answer"]
	fmt.Println("The value:", v, "Present?", ok)
}

> map[Answer:42]
> The value: 42
> The value: 48
> The value: 0
> The value: 0 Present? false

関数

関数の引数に関数を渡せる


import (
	"fmt"
	"math"
)

//引数に関数を渡すときに渡す関数の引数と戻り値の型を書かなくてはいけない
//引数に渡された関数の引数に3,4をいれる関数
func compute(fn func(float64, float64) float64) float64 {
	return fn(3, 4)
}

func main() {
	hypot := func(x, y float64) float64 {
		return math.Sqrt(x*x + y*y)
	}
	fmt.Println(hypot(5, 12))

    // hypot関数の引数に3,4をいれる
	fmt.Println(compute(hypot))
    //3の4乗を返す
	fmt.Println(compute(math.Pow))
}

> 13
> 5
> 81

クロージャー

・複数の関数がセットとなり、その関数内で変数を共有できる
・オブジェクトの変数やメソッドを容易に変更できないようにカプセル化できる

func adder() func(int) int {
   //sumの値を維持できる
	sum := 0
	return func(x int) int {
		sum += x
		return sum
	}
}

func main() {
	pos, neg := adder(), adder()
	for i := 0; i < 10; i++ {
		fmt.Println(
			pos(i),
			neg(-2*i),
		)
	}
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?