0
0

More than 5 years have passed since last update.

Goでコンストラクタっぽいことをする

Last updated at Posted at 2018-10-06

Goで構造体型の初期化をコンストラクタっぽく書く時は、およそ次のルールを守れば良い。

  • ”New型名”という名前にする。
  • 戻り値はポインタ型にする。

(例)

func NewFile(fd int, name string) *File {
    if fd < 0 {
        return nil
    }
    f := File{fd, name, nil, 0}
    return &f
}

参考:https://golang.org/doc/effective_go.html#composite_literals

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