编译器如何交叉编译为不同的OS和体系结构?

I'm very intrigued by the fact that Go (since v1.5) has in-built cross compilation options. But how is it possible to compile for a different OS and architecture? I mean that would require knowing (and probably behaving like) the target machine language and platform.

I mean that would require knowing (and probably behaving like) the target machine language and platform.

Yes, the Go compiler has to know how the target operating system works, but it doesn't need to behave like the target OS, as the Go compiler will not run the compiled executable binary, it just needs to produce it.

All the Go tools need to know is the binary formats of the different Operating Systems, and OS and architectural details (such as the instruction set, word size, endianness, alignment, available registers etc.; more info on this). And this knowledge is built into the Go tools.