JSゆるふわめも

がっこうでべんきょうしたことをめもがきしてます

メモ

文と式

    • 値を持たない
    • 値を持つ

Goのgc

最後にGCされた時に確保されたメモリの使用量の倍になった時にGCが走る

  • ex) gcした結果1Gの使用量 -> 2Gの使用量になったらgcが走る
  • GCのチューニングは可能。停止することも可能

なぜMapが毎回ランダムな値を返すのか?

  • Keyを取得して意図したValueを取得するために、Keyを明示的にソートすることを強制させるため?

サーバにアクセスした際、handlerが二回呼ばれている気がする

  • ブラウザが二回アクセスしている(最初にfavconを取りに行ってる)

公開される名前(基準は英単語の大文字・小文字だけなのか。記号・日本語だとどうなるか。)

goにおけるリテラル宣言

  • javaの場合「100」と書くと、int 100のリテラル宣言
  • goの場合、型が定まっていないintっぽい型の定数宣言として扱われる

heap, stackの割つけ

  • コンパイラが変数のスコープを見てheapにとるか、stackにとるか決定する

heapを使用する場合:変数の生存期間が関数を抜けてるため

func f() *int{
    v := new(int)
    return v
}

stackを使用する場合

func f() {
    v := new(int)
    ...
}

new

パラメータの型文メモリを確保して、ゼロ値を埋めてそのアドレスを返す

実行順序の定義 参照:Order of evaluation

Order of evaluation
At package level, initialization dependencies determine the evaluation order of individual initialization expressions in variable declarations. Otherwise, when evaluating the operands of an expression, assignment, or return statement, all function calls, method calls, and communication operations are evaluated in lexical left-to-right order.

For example, in the (function-local) assignment

y[f()], ok = g(h(), i()+x[j()], <-c), k()
the function calls and communication happen in the order f(), h(), i(), j(), <-c, g(), and k(). However, the order of those events compared to the evaluation and indexing of x and the evaluation of y is not specified.

a := 1
f := func() int { a++; return a }
x := []int{a, f()}            // x may be [1, 2] or [2, 2]: evaluation order between a and f() is not specified
m := map[int]int{a: 1, a: 2}  // m may be {2: 1} or {2: 2}: evaluation order between the two map assignments is not specified
n := map[int]int{a: f()}      // n may be {2: 3} or {3: 3}: evaluation order between the key and the value is not specified
At package level, initialization dependencies override the left-to-right rule for individual initialization expressions, but not for operands within each expression:

var a, b, c = f() + v(), g(), sqr(u()) + v()

func f() int        { return c }
func g() int        { return a }
func sqr(x int) int { return x*x }

// functions u and v are independent of all other variables and functions
The function calls happen in the order u(), sqr(), v(), f(), v(), and g().

Floating-point operations within a single expression are evaluated according to the associativity of the operators. Explicit parentheses affect the evaluation by overriding the default associativity. In the expression x + (y + z) the addition y + z is performed before adding x.

結果変数

func foo() (x, y int) {
    //x, yはfooのローカル変数0値に初期化されている
    return 100, 150 // x = 100, 150
}

//double関数が返す値を結果変数resultを用いてロギング
func double(x int) (result int) {
    defer func() { fmt.Printf("return %v", result)}
    return x + x
}

goでクロスコンパイル

ブラウザ操作を自動化するgoライブラリ

agouti

goのパッケージインポート

イメージ

go run はこれらをバックで実行させているに過ぎない。 各ツールをカスタマイズすることで(普通はしないけど、、、googleはしてる)、環境に合わせたgoの実行環境を用意することができる

レキシカルスコープの落とし穴

プログラミング言語Go P160参照

Go1.7からSSA(Statoc Single Assignment)が導入

Go1.7からSSAが導入された