result on screen Programs must declare a function named main, which will be its entrypoint Functions are declared with the keyword func. Note the reversed order in input parameters type declaration Comments follow the usual C style. Every Go program is made up of packages. Programs start running in package main A first example package main import "fmt" // Returns the result of adding two integers func add(x int, y int) int { return x + y } func main() { fmt.Println(add(42, 13)) } package main import "fmt" // Returns the result of adding two integers func add(x int, y int) int { return x + y } func main() { fmt.Println(add(42, 13)) }
a procedural language but implements objects in a very particular way, using structs, receivers and composition. class Person { public $name; private $birthDate; public function age() { return $this->birthDate->diff( new DateTime(‘now’) )->y; } } Person is the receiver of Age() type Person struct { Name string birthDay time } func (p Person) Age() int { now := time.Now() years := now.Year() - p.birthDay.Year() if now.YearDay() < p.birthDay.YearDay(){ years-- } return years }
that’s kind of a puzzle approach. A struct can include multiple other structs, acting as multiple inheritance. class Cuban extends Person { ... } class Ivan extends Cuban { ... } type Ivan struct { Person Cuban ... }
has all methods of an interface, this struct implements that interface. interface Communist { public function expropriate(); } class Ivan implements Communist { ... } type communist Interface { func expropriate() } type Ivan struct { ... } func (i Ivan) expropriate() { ... }
of execution that will execute concurrently with the calling one. Channels are the pipes that connect concurrent goroutines. You can send values into channels from one goroutine and receive those values into another goroutine.
in this case From a new goroutine, we send “ping” to the messages channel we created before Create a new channel. Channels are typed by the values they convey Concurrency: Go example func main() { messages := make(chan string) go func() { for { msg := "ping" messages <- msg fmt.Println(msg) time.Sleep(1 * time.Second) } }() for { select { case <-messages: fmt.Println("pong") } } }
them we have: • go fmt: The standard Go code style is dictated by this tool. The format this tool outputs is the accepted format of the source code. • go test: Go comes with a simple testing tool, including code coverage. • go doc: Generates documentation from source code and comments.