Skip to content

Instantly share code, notes, and snippets.

@hagino3000
Last active January 6, 2016 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hagino3000/81dc6e0ba2d8d71d4ce0 to your computer and use it in GitHub Desktop.
Save hagino3000/81dc6e0ba2d8d71d4ce0 to your computer and use it in GitHub Desktop.
The Go Programming Language chap.2 (http://www.gopl.io/)

2.1 Names

名前は文字(unicode) or アンスコ始まり

キーワード25個

  • yieldとかclassとかない
  • chanある

nilやnewの予約語は36ぐらい

  • これらは上書きできる
    • 混乱するかもね (するだろ)
    • nil := 100
  • 関数の中で宣言されたエンティティはローカル
  • 外はパッケージビジブル
  • さらに頭を大文字にするとパッケージを跨いで参照できる
  • 名前の長さに制約は無い
    • けど短かい名前を推奨
    • 特にローカルでスコープの小さい奴
  • camelCase使う
    • HTMLみたいな略語はHtmlにしない
    • escapeHTMLもしくはhtmlEscape

2.2 Declarations

  • var, const, type, func
  • .goで名前が終るファイルにプログラム書く
  • package宣言で初める
  • 次にimport

関数宣言のしかた

func fToC(f float64) float64 {
    return (f - 32) * 5 / 9
}

抜粋:: Brian W. Kernighan. “The Go Programming Language (Takashi Nishibayashi's Library)”。 iBooks.

2.3 Variables

= expression と type は省略できるよ expressionを省略すると

var i, j, k int
var name string

typeを省略すると

var b, f, s = true, 2.3, “four"

2.3.1 Short Variable Declarations

関数の中でだけ使える

Because of their brevity and flexibility, short variable declarations are used to declare and initialize the majority of local variables.

抜粋:: Brian W. Kernighan. “The Go Programming Language (Takashi Nishibayashi's Library)”。 iBooks.

少なくとも1個は新しい変数をleft sideにおく必要がある

2.3.2 Pointers

Pointerはアドレスです

x := 1
p := &x
// 値の参照は
fmt.Println(*p)

Pointerのzero valueはnil

Pointers are key to the flag package, which uses a program’s command-line arguments to set the values of certain variables distributed throughout the program.

抜粋:: Brian W. Kernighan. “The Go Programming Language (Takashi Nishibayashi's Library)”。 iBooks.

argparseみたいなやつ

2.3.3 new function

名前をつけなくていい

2.3.4 Lifetime of Variables

The lifetime of a package-level variable is the entire execution of the program.

抜粋:: Brian W. Kernighan. “The Go Programming Language (Takashi Nishibayashi's Library)”。 iBooks.

はい。
一方でlocal variableはunreachableになるまで

How does the garbage collector know that a variable’s storage can be reclaimed? The full story is much more detailed than we need here, but the basic idea is that every package-level variable, and every local variable of each currently active function, can potentially be the start or root of a path to the variable in question, following pointers and other kinds of references that ultimately lead to the variable. If no such path exists, the variable has become unreachable, so it can no longer affect the rest of the computation.

抜粋:: Brian W. Kernighan. “The Go Programming Language (Takashi Nishibayashi's Library)”。 iBooks. 

マークアンドスイープっぽい事をいってる?

2.4 Assignments

2.4.1 Tuple Assignments

  • スワップできる x, y = y, x 多値返却 値を捨てるにはアンダースコア、gofmtで警告がでない

2.4.2 Assignability

2.5 Type Declarations

  • C言語のtypedef
  • T.(x) で変換
  • CelsiusとFharenheitの冷
  • オペレーター適用時に型チェックができる C + F // error
  • 型にStringメソッドを実装するとPrintlnで使われる
  • 文字列結合できる?

2.6 Packages and Files

2.7 Scope

  • Scopeはコンパイル時

  • lifetimeは実行時

  • ブロックスコープあるよ

  • 変数の名前解決が書いてる

  • forは二つのブロックを作るよ、ループ本体と条件

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment