関連記事
GitHub - devlights/blog-summary: ブログ「いろいろ備忘録日記」のまとめ
概要
以下、自分用のメモです。
Go 1.19からビルドタグにunixが指定出来るようになりました。
で、このタグを指定した場合 UNIXライクなOS が条件にマッチすると記載されています。
go command - cmd/go - Go Packages
"unix", if GOOS is a Unix or Unix-like system.
では、このUNIXライクなOSって具体的にはどれがマッチするの?ってなります。
このマッピングは $(go env GOROOT)/src/cmd/dist/build.go の中に以下のように定義されています。
// unixOS is the set of GOOS values matched by the "unix" build tag. // This is the same list as in internal/syslist/syslist.go. var unixOS = map[string]bool{ "aix": true, "android": true, "darwin": true, "dragonfly": true, "freebsd": true, "hurd": true, "illumos": true, "ios": true, "linux": true, "netbsd": true, "openbsd": true, "solaris": true, }
https://cs.opensource.google/go/go/+/refs/tags/go1.25.1:src/cmd/dist/build.go;l=1070
なので、linux, macOS(darwin) の両方に対応したい場合は unix を指定したら問題ないってことですね。
サンプル
main_unix.go
//go:build unix // Go 1.19 で Build Constraints に新たに unix が追加された。 // // - https://go.dev/doc/go1.19#go-unix // // 実際のマッピングは $(go env GOROOT)/src/cmd/dist/build.go に以下のように定義されている。(Go 1.25) // // var unixOS = map[string]bool{ // "aix": true, // "android": true, // "darwin": true, // "dragonfly": true, // "freebsd": true, // "hurd": true, // "illumos": true, // "ios": true, // "linux": true, // "netbsd": true, // "openbsd": true, // "solaris": true, // } // // なので、linux, macOS(darwin) の両方で有効にする場合は unix を指定すれば良い。 // // # REFERENCES // - https://cs.opensource.google/go/go/+/refs/tags/go1.25.1:src/cmd/dist/build.go;l=1070 // - https://dev.to/emreodabas/quick-guide-go-119-features-1j40 // - https://go.dev/doc/go1.19#go-unix package main import "fmt" func main() { fmt.Println("run on unix") }
実行結果
$ task task: [run] go run . run on unix
参考情報
Goのおすすめ書籍
過去の記事については、以下のページからご参照下さい。
サンプルコードは、以下の場所で公開しています。