Emacs の キーボードマクロ みたいなもの。
↓の例だとキーボードショートカットが割り当てられている機能の組み合わせでしかマクロを組み立てられないように見えるが、普通のカーソル移動なども記録してくれる。
実行例
関数名をコピーして定型のコメントを挿入する例。 type の羅列に対して実行したりすると便利。
Emacs の キーボードマクロ みたいなもの。
↓の例だとキーボードショートカットが割り当てられている機能の組み合わせでしかマクロを組み立てられないように見えるが、普通のカーソル移動なども記録してくれる。
関数名をコピーして定型のコメントを挿入する例。 type の羅列に対して実行したりすると便利。
A Tour of Go: Named return values にあるように、関数の戻り値の型に加えて変数名も書ける。これを named return values と呼ぶ。
Named return values を使っている関数内では引数無しで return
できる。これを naked return と呼ぶ。
しかし、関数定義で named return values を使う場合、全ての戻り値に変数名を指定する必要がある。以下のコードは syntax error: mixed named and unnamed function parameters
エラーになる。
package main import ( "fmt" ) func f() (s string, error) { return "hi", nil } func main() { fmt.Println("Hello, playground") f() }
https://play.golang.org/p/LDj7gyZdl_f
エラーを回避するには変数名を指定すれば良い。どうしても変数名を指定したくない場合は _
が使える。
package main import ( "fmt" ) func f() (s string, _ error) { return "hi", nil } func main() { fmt.Println("Hello, playground") f() }
https://play.golang.org/p/Xeo14U5o1sN
Yes, the form for eliding individual names requires and explicit _
func (_ int, y int) int
&string(byteArray)
のように直接ポインタを得ることはできない。変数に代入してから & 演算子を使う必要がある。
package main import "fmt" func myLen(s *string) int { return len(*s) } func main() { b := []byte(`hello`) //NG: cannot take the address of string(b) //n := myLen(&string(b)) //OK tmp := string(b); n := myLen(&tmp) fmt.Println(n) }
https://play.golang.org/p/d3cDdKmTu-l
The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array. As an exception to the addressability requirement, x may also be a (possibly parenthesized) composite literal.
型キャスト, type casting, type conversions
2023-08-19 追記: 改良版を書いた。
WEBrick::HTTPServer.new()
に SSLEnable: true
と SSLCertName
を渡すSSLCertName: [['CN', WEBrick::Utils.getservername]]
とすると起動時に証明書を自動生成するsrv = WEBrick::HTTPServer.new({ :DocumentRoot => './', :BindAddress => '127.0.0.1', :Port => 8080, SSLEnable: true, SSLCertName: [['CN', WEBrick::Utils.getservername]]}) # ローカルマシン上で実行する場合 [['CN', 'localhost']] でも可 srv.mount_proc('/') { |req, res| r = Hash[URI.decode_www_form(req.request_uri.query)] res.body = r.inspect } trap("INT"){ srv.shutdown } srv.start
即席で作られた自己証明書なので、
-k
オプションが必要
curl -k https://localhost:8080
参考
このように表示されていたものが、
このように、あたかも Ruby の式展開(string interpolation)のように表示されることがある。
これは「コードの折り畳み(Code Folding)」によるもの。マウスクリックか Cmd + "+" (Cmd と + を同時に押す)で上の見た目に展開できる。逆に下の見た目にしたいときは Cmd + "-" で折り畳める。
Preferences -> Editor -> General -> Code Folding -> Go -> Format strings のチェックを外すとこの挙動をオフにできる(常に上の見た目を維持する)。