Golang: appendの挙動
· ☕ 1 min read
1 2 s := append(str[:j], hoge...) s = append(s, str[j:]...) こういうコードでバグるときがある そもそもappendとはどういうものか? append(str[:j](https://scrapbox.io/yuwd/%3Aj),hoge...)をすると, appendはまずcap(str)を見る cap(str)にhogeが入りきらないとわかると, より長い配列を作成 ...

· ☕ 1 min read
Goにはtry-catchがない なぜか? First, there is nothing truly exceptional about errors in computer programs. For instance, the inability to open a file is a common issue that does not deserve special linguistic constructs; if and return are fine. 1 2 3 4 f, err := os.Open(fileName) if err != nil { return err } Also, if errors use special control structures, error handling distorts the control flow for a program that handles errors. The Java-like style of try-catch-finally blocks interlaces multiple overlapping flows of control that interact in complex ways. Although in contrast Go makes it more verbose to check errors, the explicit design keeps the flow of control straightforward—literally. There is no question the resulting code can ...