How Go is Platform Independent

I am new to Go.

How go is platform independent?
Because go creates a machine code which is platform dependent and runs that.

Sorry if I am wrong.

Any links that helps me to know how Go runs a program.

Thanks.

1 Like

By setting GOOS and GOARCH environment variables the compiler will produce executable for the destination platform.

Thant means suppose If I have helloworld.go program compiled in windows.
In windows Itself I have to change those env variables (to whatever platform I am going to run ) and compile the code and copy the machine code to that system and run it.

Is that what you told? or I understood it in the wrong direction.

Thanks.

Sort of. For your current platform you compile with

go build

For any other platform you have to prefix the destination platform

GOOS=linux go build

Once compiled, you can run on the destination platform ./main

Search for GOOS and GOARCH for correct syntax.

Thanks.

I am looking to learn about how go build works.
Do you have any links that gives more info about that.

Sadly this syntax doesn’t work on windows (unless using a POSIX conforming shell).

Windows users have to use set GOOS=linux once in the terminal session they want to use to create linux builds.

Still this might cause problems, as windows usually doesn’t have a compatible libc installed. Therefore linking it might fail.

CCing from windows to linux isn’t easy, and go does not change anything.

Go is platform independent in the regard, that you can compile your source code on any system and it will just work there (assuming its pure go and does not use any FFI).

You can also find a list of GOOS/GOARCH combinations here:

https://golang.org/doc/install/source#environment

As a principle it should work…

As of I know, In any language If you have source code you can compile it in any platform.

Thnaks

In some languages you need to take special care if you want the program to be compilable on any platform.

Some things you can simply adhere to as a developer (eg. using fixed with integer types rather than random size int in C) or by choosing libraries that abstract away operating system specifics.

And even in go, as soon as you use the os package, this platform indendency goes away, but at least its obvious then.

So that means Go is not platform independent like java where machine code works on all platforms. But go source code an be compiled in any platform.

Thanks

Hi, @sandy, I would say Go is platform-independent just like including stdio.h is “platform-independent” in C. Implementation details differ, but the “public interface” (e.g. fopen, fclose, fseek, etc.) are all standardized.

Go is not platform independent in the way that Java is and nor does it want to be. I Google’ed site:golang.org AND (platform independent) and got relatively few results (128). One of the top results was the os package, where, like OS-related functions in C (such as operations on FILE*s that I mentioned earlier), the public interface is standardized (e.g. os.Open, os.Create, the underlying datatypes differ depending on the host OS.

Hi,

Exactly, Golang has a cross compiler but is not plaftform independent. If you look at https://godoc.org/golang.org/x/sys/windows and compare to https://godoc.org/golang.org/x/sys/unix, you’ll observe many differences (I let you spot them, there are plenty). So code is not independent at the end, but you can compile it from any platform.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.