Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating and running go build for Mac only?

Tags:

bash

shell

macos

go

I am into a situation in which I have to run Go build on Mac OS/OS X. Build will be generated from Linux operating system & that build I have to run on Mac OS/OS X.

I have tried to generate a cross-platform build for Mac using below command and build is generated.

env GOOS=linux GOARCH=amd64 go build

This generated a Go build but I move this build to Mac it shows .dms file extension.

Now I have two questions

  • Am I generating the right build for Mac?
  • How do I run this?DMS file on Mac?
like image 419
TechChain Avatar asked Jan 29 '19 09:01

TechChain


People also ask

How do I set up go environment on Mac?

Open Visual Studio Code, navigate to code > preferences > extensions and search for the Go for Visual Studio Code extension. The extension depends on some extra command-line tools. If they are missing you get to see a pop-up, the first time you open a directory or workspace which contains Go code. Click Install All .

How do I make a go program executable?

From the command line in the hello directory, run the go build command to compile the code into an executable. From the command line in the hello directory, run the new hello executable to confirm that the code works.

How can you make a file build only on Windows in go?

Simply put, if your source file includes the suffix, _$GOOS.go then it will only be built on that platform. All other platforms will behave as if the file is not present. The same applies for _$GOARCH.go . The two can be combined as _$GOOS_$GOARCH.go , but not _$GOARCH_$GOOS.go .


1 Answers

Since your binary will target OSX, you need to set GOOS to darwin, so your command will be

env GOOS=darwin GOARCH=amd64 go build

Documentation on compiler env vars is here: https://golang.org/doc/install/source#environment

To run the binary on mac you need to make sure that binary is executable:

chmod +x path-to-binary

and then run it in terminal:

path-to-binary
like image 147
Nebril Avatar answered Oct 27 '22 01:10

Nebril