@kyanny's blog

My thoughts, my life. Views/opinions are my own.

GoLand にもキーボードマクロがある

Emacs の キーボードマクロ みたいなもの。

↓の例だとキーボードショートカットが割り当てられている機能の組み合わせでしかマクロを組み立てられないように見えるが、普通のカーソル移動なども記録してくれる。

pleiades.io

実行例

関数名をコピーして定型のコメントを挿入する例。 type の羅列に対して実行したりすると便利。

f:id:a666666:20200902231603g:plain

Go: named return values, naked return, mixed named and unnamed function parameters

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

Go: 型変換した値を変数に入れずにアドレスを得ることはできない

&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

Ruby: WEBrick で HTTPS サーバを立てる

2023-08-19 追記: 改良版を書いた。

blog.kyanny.me


  • WEBrick::HTTPServer.new()SSLEnable: trueSSLCertName を渡す
  • 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

即席で作られた自己証明書なので、

参考

GoLand: fmt.Printf() の中身は折り畳める

このように表示されていたものが、

f:id:a666666:20200828145350p:plain

このように、あたかも Ruby の式展開(string interpolation)のように表示されることがある。

f:id:a666666:20200828145608p:plain

これは「コードの折り畳み(Code Folding)」によるもの。マウスクリックか Cmd + "+" (Cmd と + を同時に押す)で上の見た目に展開できる。逆に下の見た目にしたいときは Cmd + "-" で折り畳める。

Preferences -> Editor -> General -> Code Folding -> Go -> Format strings のチェックを外すとこの挙動をオフにできる(常に上の見た目を維持する)。

f:id:a666666:20200828145908p:plain