How to build differently for Linux and Windows

I look at the Go language. And I have a question: for example, I will create a new library that should use methods from one Go package for Windows and from others for Linux.

I'm just asking if there is a convenient way to organize the build process? Of course, I can just create a project for each OS and change the import name for each OS.

+4
source share
1 answer

Use build restrictions and file names. See Creating a Package . Source Code The os package contains many examples: https://golang.org/src/os/ . Go Source code: https://go.googlesource.com/go or https://github.com/golang/go .

Build resistance for Unix:

// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris

Some assembly file names:

stat_darwin.go     stat_linux.go   stat_openbsd.go  stat_unix.go
stat_dragonfly.go  stat_nacl.go    stat_plan9.go    stat_windows.go
stat_freebsd.go    stat_netbsd.go  stat_solaris.go

The Go tools and the standard library started by using the assembly file name, and then, as the requirements became more complex, they began to use assembly restrictions.

+5
source

Source: https://habr.com/ru/post/1684580/


All Articles