golang supports cross-platform. Due to its modular design and modularity, i.e. code is compiled and converted into the smallest possible binary form, golang requires no dependencies; its code can be compiled on any platform and can be used on any server and application Compile. Moreover, the Go language has its own linker and does not rely on the compiler and linker provided by any system; therefore, the compiled binary executable file can run in almost any system environment.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
golang supports cross-platform.
#One of the characteristics of golang is: platform independence (cross-platform compilation).
Go language, like Java language, supports platform independence. Due to its modular design and modularity, i.e. the code is compiled and converted into the smallest possible binary form, therefore, it requires no dependencies. Its code compiles on any platform and on any server and application.
No need to use a virtual machine, the Go language code can be directly output as a binary executable file. Moreover, the Go language has its own linker and does not rely on the compiler and linker provided by any system. Therefore, the compiled binary executable file can run in almost any system environment.
Golang, like C/C, is compiled into platform-related binary files, so when developing with golang, you also need to consider the issue of cross-platform support. This article briefly summarizes how golang solves cross-platform problems.
GOOS and GOARCH
First of all, you must understand the two runtime variables defined by golang in the runtime package:
runtime.GOOS
##runtime.GOARCH
// GOOS is the running program's operating system target: // one of darwin, freebsd, linux, and so on. const GOOS string = sys.GOOS // GOARCH is the running program's architecture target: // one of 386, amd64, arm, s390x, and so on. const GOARCH string = sys.GOARCH
package main import ( "fmt" "runtime" ) func main() { fmt.Printf("OS: %s, ARCH: %s\n", runtime.GOOS, runtime.GOARCH) }
Cross compilation problem
The compilation of golang program is very Simple, just use go build without considering any compilation options. For example, the following command will generate a binary file named "helloworld" in the current directory:go build helloworld.go
$ GOOS=darwin GOARCH=amd64 go build helloworld.go
How to make your package support multiple platforms?
Join you using golang to develop a package for other people to use, so how do you make your package support multiple platforms? In fact, usually, developers do not need to consider this issue when developing general applications, because golang's standard library shields the underlying details.If you want to release a binary version, then you only need to release a version that supports multiple platforms through the above cross-compilation.If the golang program you develop uses relatively low-level system calls (of course, this is usually not recommended), and the system calls of different platforms are different, then you need to consider supporting multiple platforms problem. You can dynamically determine the values of runtime.GOOS and runtime.GOARCH in the program, and then handle it through if-else if or switch case statements; but this is not a desirable method because it is not conducive to maintenance and makes the code look ugly. The recommended approach is to put the implementations for different platforms in different files, and then tell the golang compiler what platform each file corresponds to. There are two methods here. The first method is to indicate it by the file name. The file name pattern is as follows:
*_[GOOS]_[GOARCH].go
yourfile_darwin.go yourfile_linux.go yourfile_windows.go
另外一种办法是通过一个特殊的注释。例如,假设你想使yourfile.go只在linux平台时才会编译,那么在文件头加上"+build linux"即可:
// +build linux ......
这里一定要注意,"+build linux"必须在所有代码的前面,但这条注释之前可以有空行或其它注释。它之后必须有一个空行。
这种通过注释的方式和通过文件名标示的方式作用相同,但是文件名的方式只能支持一个平台,而注释的方式可以标示一个文件同时支持多个平台,例如下面的注释标示该文件同时支持freebsd, openbsd和netbsd这三个平台:
// +build freebsd openbsd netbsd
注意多个平台之间用空格隔开时表示"或"的关系,如果用逗号隔开则表示"与"的关系,例如下面的注释表示 (linux AND 386) OR darwin:
// +build linux,386 darwin
这里只是介绍了最基本的使用场景,实际使用中,应该充分发挥软件设计的各种思想灵活使用。例如虽然针对不同的平台有不同的实现,但给上层客户端应用程序提供API应该统一。
The above is the detailed content of Does golang support cross-platform?. For more information, please follow other related articles on the PHP Chinese website!