How can you cross-compile a Go program for different platforms using the Go toolchain?
- Use the "go cross-compile" command.
- Use the "go build" command with the "-o" flag and specify the target platform.
- Use the "go run" command with the "-target" flag followed by the desired platform.
- Use the "gox" third-party tool for cross-compilation.
To cross-compile a Go program, you can use the "go build" command with the "-o" flag followed by the desired output file name and the target platform. For example, to compile for Linux from a Windows machine, you can run: GOOS=linux GOARCH=amd64 go build -o myapp-linux. This will create an executable for Linux on an AMD64 architecture. This approach leverages the Go toolchain's built-in support for cross-compilation.
Loading...
Related Quiz
- What is the built-in error type in Go and how is it generally used?
- How would you handle a situation where multiple Goroutines are attempting to access a shared resource?
- How would you create a custom HTTP handler struct in Go?
- Describe how you would write data to a file, ensuring that the file is properly closed afterward.
- What is the difference between an array and a slice in Go?