Search code examples
gccgocompilationcompiler-constructioncross-compiling

Why does Go source contain many .go files? How do they get compiled?


i want know how go compiler work!

https://github.com/golang/go this source Contains 88% .go file.

so this should be have another compiler to execute .go file.

Example : https://github.com/golang/go/blob/964639cc338db650ccadeafb7424bc8ebb2c0f6c/src/go/ast/ast.go

Golang use what compiler for final Generate Execute file?! and where available they source?

may be Golang generate a c code and next use GCC or ...?

New Update

i not want go1.4 then in using c.

88% source of github.com/golang/go is .go file. what compile .go file at Go Source? i want see Final GO Compiler?

https://github.com/golang/go/search?l=c

i think this called cg.


mean An old version of Go compiler (version 1.4) is used to compile newer version of Go compiler.???!

go-go1.4.3/src/go/token/token.go this is go token,lexer and is write at GO

FUNC: "func", GO: "go", GOTO: "goto", IF: "if", IMPORT: "import",

so where main compiler for execute .go file?

go-go1.4.3/src/runtime/compiler.go

// Copyright 2012 The Go Authors.  All rights reserved.
package runtime
// Compiler is the name of the compiler toolchain that built the
// running binary.  Known toolchains are:
//
//  gc      The 5g/6g/8g compiler suite at code.google.com/p/go.
//  gccgo   The gccgo front end, part of the GCC compiler suite.
//
const Compiler = "gc"

go-go1.4.3/src/cmd/gc$ make

go tool dist install -v
make: go: Command not found
../../Make.dist:13: recipe for target 'install' failed
make: *** [install] Error 127

make gc why need Go?!


Last version of GO Language how work?! mean how this generate a output for cross platform? this use generate code to C and then use c compiler?


Solution

  • According to https://golang.org/doc/go1.5#implementation and https://www.infoq.com/news/2015/01/golang-15-bootstrapped , Go was originally implemented in C but became self-hosted at version 1.5. Bootstrapping is the process of compiling a compiler with itself. The Go developers wrote a C -> Go transpiler to convert the original C compiler to Go, then they hand-edited the Go code to make it idiomatic.