Does golang support cross-platform?

青灯夜游
Release: 2023-01-12 17:14:56
Original
3846 people have browsed it

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.

Does golang support cross-platform?

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 It is the operating system of the target machine where the golang program is running, and GOARC is the architecture of the target machine where the golang program is running. Their values are determined when the program is compiled. The definitions of these two runtime variables (actually constants, see below) are as follows:

// 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
Copy after login

You can dynamically obtain these two at runtime through the following program Value,


package main import ( "fmt" "runtime" ) func main() { fmt.Printf("OS: %s, ARCH: %s\n", runtime.GOOS, runtime.GOARCH) }
Copy after login

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
Copy after login

But the problem is, if the current compilation The machine's OS is linux and ARCH is amd64, so the compiled binary cannot run on Mac OS. You can recompile it once on Mac OS to produce a binary file for Mac OS. But in this case, it is necessary to prepare a compilation environment for all target platforms, which is obviously not an effective method.

In fact, solving this problem is very simple, just use the two runtime variables mentioned above. For example, if you only have a Linux compilation environment, but want to generate a binary file that can run on Mac OS, you only need to set two environment variables. If the target OS is darwin and ARCH is amd64, then use the following command to compile:

$ GOOS=darwin GOARCH=amd64 go build helloworld.go
Copy after login

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
Copy after login

Both GOOS and GOARCH are optional in the above file name. For example, assuming you have different implementations for Mac OS, Linux and Windows platforms, you can name these three files as follows:

yourfile_darwin.go yourfile_linux.go yourfile_windows.go
Copy after login

Then when your target platform is linux, only yourfile_linux.go will be compiled. If there are neither GOOS nor GOARCH in the file name (for example, yourfile.go), then the default is to always be compiled.

另外一种办法是通过一个特殊的注释。例如,假设你想使yourfile.go只在linux平台时才会编译,那么在文件头加上"+build linux"即可:

// +build linux ......
Copy after login

这里一定要注意,"+build linux"必须在所有代码的前面,但这条注释之前可以有空行或其它注释。它之后必须有一个空行。

这种通过注释的方式和通过文件名标示的方式作用相同,但是文件名的方式只能支持一个平台,而注释的方式可以标示一个文件同时支持多个平台,例如下面的注释标示该文件同时支持freebsd, openbsd和netbsd这三个平台:

// +build freebsd openbsd netbsd
Copy after login

注意多个平台之间用空格隔开时表示"或"的关系,如果用逗号隔开则表示"与"的关系,例如下面的注释表示 (linux AND 386) OR darwin:

// +build linux,386 darwin
Copy after login

这里只是介绍了最基本的使用场景,实际使用中,应该充分发挥软件设计的各种思想灵活使用。例如虽然针对不同的平台有不同的实现,但给上层客户端应用程序提供API应该统一。

【相关推荐:Go视频教程编程教学

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!