Go1.4及以前的版本是从C语言通过gcc编译得到可执行文件go的,而从1.5之后是采用bootrap方式以1.4的可执行文件go为编译工具通过go build *.go来编译得到新版本的可执行文件go的。而对于操作系统和指令集的获取是在gcc编译C语言文件的时候实现的,例如在unix.c文件里,

  • 通过gcc的预定义的宏来编译不同的gohostos变量赋值代码
  • 通过uname系统函数来获取machine字段,并判断其中包含的特征字符串来判断系统指令集

编译期间会把获取到的操作系统和指令集名称字符串编译成常量。开发的时候可以通过读取runtime.GOOS和runtime.GOARCH两个常量来获取

unix.c的main函数代码:

int
main(int argc, char **argv)
{
        Buf b;
        int osx;
        struct utsname u;

        setvbuf(stdout, nil, _IOLBF, 0);
        setvbuf(stderr, nil, _IOLBF, 0);

        setenv("TERM", "dumb", 1); // disable escape codes in clang errors

        binit(&b);

        slash = "/";

#if defined(__APPLE__)
        gohostos = "darwin";
        // Even on 64-bit platform, darwin uname -m prints i386.
        run(&b, nil, 0, "sysctl", "machdep.cpu.extfeatures", nil);
        if(contains(bstr(&b), "EM64T"))
                gohostarch = "amd64";
#elif defined(__linux__)
        gohostos = "linux";
#elif defined(__DragonFly__)
        gohostos = "dragonfly";
#elif defined(__FreeBSD__)
        gohostos = "freebsd";
#elif defined(__FreeBSD_kernel__)
        // detect debian/kFreeBSD.
        // http://wiki.debian.org/Debian_GNU/kFreeBSD_FAQ#Q._How_do_I_detect_kfreebsd_with_preprocessor_directives_in_a_C_program.3F
        gohostos = "freebsd";
#elif defined(__OpenBSD__)
        gohostos = "openbsd";
#elif defined(__NetBSD__)
        gohostos = "netbsd";
#elif defined(__sun) && defined(__SVR4)
        gohostos = "solaris";
        // Even on 64-bit platform, solaris uname -m prints i86pc.
        run(&b, nil, 0, "isainfo", "-n", nil);
        if(contains(bstr(&b), "amd64"))
                gohostarch = "amd64";
        if(contains(bstr(&b), "i386"))
                gohostarch = "386";
#else
        fatal("unknown operating system");
#endif

        if(gohostarch == nil) {
                if(uname(&u) < 0)
                        fatal("uname: %s", strerror(errno));
                if(contains(u.machine, "x86_64") || contains(u.machine, "amd64"))
                        gohostarch = "amd64";
                else if(hassuffix(u.machine, "86"))
                        gohostarch = "386";
                else if(contains(u.machine, "arm"))
                        gohostarch = "arm";
                else
                        fatal("unknown architecture: %s", u.machine);
        }

        if(streq(gohostarch, "arm"))
                maxnbg = 1;

        // The OS X 10.6 linker does not support external linking mode.
        // See golang.org/issue/5130.
        //
        // OS X 10.6 does not work with clang either, but OS X 10.9 requires it.
        // It seems to work with OS X 10.8, so we default to clang for 10.8 and later.
        // See golang.org/issue/5822.
        //
        // Roughly, OS X 10.N shows up as uname release (N+4),
        // so OS X 10.6 is uname version 10 and OS X 10.8 is uname version 12.
        if(streq(gohostos, "darwin")) {
                if(uname(&u) < 0)
                        fatal("uname: %s", strerror(errno));
                osx = atoi(u.release) - 4;
                if(osx <= 6)
                        goextlinkenabled = "0";
                if(osx >= 8)
                        defaultclang = 1;
        }

        init();
        xmain(argc, argv);
        bfree(&b);
        return 0;
}