Go was created in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson as a 20% project at Google. It was designed for concurrency, simplicity, and performance. A Go program consists of packages, with each Go file belonging to one package. The main package contains the main function that serves as the program entry point. Packages can import functions and objects from other packages through import statements. Visibility and aliasing rules determine which objects are accessible from other packages. The general structure of a Go program includes package-level constants, variables, and types declared after imports, along with functions and methods.
2. Beginning
Go started on September 21, 2007 as a 20% part-time project at Google Inc.
by three distinguished IT-engineers:
● Robert Griesemer (known for his work at the Java HotSpot Virtual Machine),
● Rob ‘Commander’ Pike (member of the Unix team at Bell Labs, worked at the
Plan 9 and Inferno operating systems and the Limbo programming language)
● Ken Thompson (member of the Unix team at Bell Labs, one of the fathers of C,
Unix and Plan 9 operating systems, co-developed UTF-8 with Rob Pike)
4. Packages
● a program is constructed as a “package” - pkg
● Every go-file belongs to one (and only one) package
● Many different .go files can belong to one package, so the
filename(s) and package name are generally not the same
● A standalone executable belongs to package main.
● Each Go application contains one package called main
● A package name is written in lowercase letters
5. Packages
● Go installation contains a number of ready-to-use packages, “Standard
Library”
● Package dependencies determine the order in which to build packages
● The package is compiled as a unit
● A Go program is created by linking together a set of packages through
the import keyword.
● import “fmt” tells Go that this program needs (functions, or other
elements, from) the package fmt
6. Packages
● If multiple packages are needed, they can each be imported by a
separate statement:
import “fmt”
import “os”
Or in a single line :
import “fmt”; import “os”
7. Packages
● A shorter and more elegant way (called factoring the keyword, also
applicable to const, var and type) is available:
import (
“fmt”
“os”
)
● If package name starts with ./ the package is searched in the actual
directory; starting with / , otherwise in global GO tree
8. Visibility in Packages
● Consider
import (
“pkg1”
)
● pkg1.Id ⇒ Visible in code outside the package
● pkg1.id ⇒ Not visible in code outside the package
● Id is used as pkg1.Id
● constants (const), variables (var), and types (type) can be declared
after the import statement ⇒ “global” having package scope
9. Packages
● An alias can also be used, like
import pk “pkg1”
● used as pk.Id
import (
“fmt”
“os”
)
● An imported package which is not used in the rest of the code is a
build-error ⇒ “no unnecessary code! ”
10. package main
import (
“fmt”
)
const c = “C”
var v int = 5
type T struct{}
func init() { // initialization of package
}
func main() {
var a int
Func1()
// ...
fmt.Println(a)
}
func (t T) Method1() {
//...
}
func Func1() { // exported function
Func1 //...
}
General structure
of
GO program
11. References
● “The Way to Go: A Thorough Introduction to the Go Programming Language”, Ivo Balbaert
● “The Go Programming Language”, Alan Donovan, Brian Kernighan, Addison-Wesley