Pros and cons of Golang for DevOps Development (six): Cross-platform compilation

Source: Internet
Author: User
Benefits of Golang: compiling Windows programs under Linux
For people like me who mostly use Linux, I feel very painful about having to deal with problems under Windows occasionally. This sentence was especially correct when I wrote our Smart Agent ™. It can run on Linux and Windows at the same time, and will delve into the underlying related issues of these two systems for our log management and monitoring software.

Because our agent is written in Golang, it is very easy to compile the code into a program that can run on Windows under the Linux environment. Most of the work is done by two parameters passed in when running the go build command: GOARCH and GOOS.



You can run go tool dist list to see all the combinations of these two parameters. There are 38 combinations under Go 1.8. The following example shows how to compile programs for Linux and Windows on AMD64 and Intel i386 architectures, and you can easily see how to create a Makefile to easily build programs for various systems.

GOOS = linux GOARCH = amd64 go build -o bin / myapp_linux_amd64 myapp
GOOS = windows GOARCH = amd64 go build -o bin / myapp_windows_amd64 myapp
GOOS = linux GOARCH = 386 go build -o bin / myapp_linux_386 myapp
Cgo
If your project uses cgo, you may have a little trouble. In order for cgo code to compile cross-platform, you need to have the correct toolchain installed in the compilation environment. Although it's been a while since I last went face-to-face with gcc, it's easy to find the right command to install this toolchain on an Unbuntu 16.04 machine. Here are some one-liners that configure your cgo compilation environment:

# Install cgo dependencies
apt-get install -y gcc libsystemd-dev gcc-multilib
# cgo for linux / 386
apt-get install -y libc6-dev-i386
# cgo for windows
apt-get install -y gcc-mingw-w64
The compile instructions that have changed are as follows:

CGO_ENABLED = 1 GOOS = linux GOARCH = amd64 CC = gcc go build -o bin / myapp_linux_amd64 myapp
CGO_ENABLED = 1 GOOS = windows GOARCH = amd64 CXX = x86_64-w64-mingw32-g ++ CC = x86_64-w64-mingw32-gcc go build -o bin / myapp_windows_amd64 myapp
CGO_ENABLED = 1 GOOS = linux GOARCH = 386 CC = gcc go build -o bin / myapp_linux_386 myapp
Golang cons: official documentation on the Windows part
Golang's official documentation site does a great job. There are many practical examples above, links to source code, and of course a playground for testing small pieces of code. However, a flaw in the official golang documentation will become apparent when you use some code that behaves differently under Windows.

Some pages, such as os, do a great job explaining the differences between many functions on Unix and Windows systems. But I was curious why they wrote a comment similar to the following while the standard library under Windows includes functions like Getegid:

Getegid returns the effective group id of the caller.
Under Windows, the function returns -1.
Rather than let me figure out that this return value is meaningless under Windows, I would rather the compiler fail to compile automatically after it finds that the target system is Windows, otherwise this function does nothing.

Other such Windows-related pages are completely empty and useless statements, such as the exec page:

Note that the examples in this package only work on Unix systems. They do not run on Windows and golang.org and Go Playground on godoc.org.
Golang con: Windows compatibility for developer community third-party packages
This shortcoming only becomes apparent when you have tried to compile a Windows program because you can easily compile it across platforms after you have developed a feature under Unix. A few times before when I used some open source libraries to develop a feature for Linux systems, I accidentally messed up Windows programs-because these libraries used some Unix-specific calls. Here are two relatively simple solutions:

1. Contributing to the open source community
If you have the time and want to contribute to these projects, create a PR that supports Windows! Note: Depending on your development environment, this can be very time consuming, because while compiling Windows programs is easy, testing them is not easy.

2. Use Build Constraints
In Golang, it is easy to use build constraints to exclude or include various files at compile time. For example, to include a Windows-only dependency in a code file compiled for NT only, you just need to do this:

// + build windows

package mypackage

import "github.com/bluematador/windows-only"
You can do the reverse-exclude unsupported GOOS:

// + build! windows

package mypackage

import "github.com/bluematador/linux-only"
The great thing is that it allows you to sort out your code more, and allows you to have a function that can be called on both systems but behaves differently through build constraints in the source code.

via: https://blog.bluematador.com/golang-pros-cons-part-5-cross-platform-compiling

Author: Matthew Barlocker Translator: p31d3ng proofread: polaris1119

This article was originally compiled by GCTT.

This article was originally translated by GCTT and first published on Go.com. Also want to join the ranks of translators and make some contributions to open source? Welcome to GCTT!
Translation work and translation publication are for learning and communication purposes only. Translation work complies with the CC-BY-NC-SA agreement. If our work violates your rights, please contact us in time.
Welcome to reprint in accordance with the provisions of the CC-BY-NC-SA agreement, please mark and retain the original / translation link and the author / translator in the text.
The article only represents the author's knowledge and opinions. If you have different opinions, please queue downstairs.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.