DevNexus 2019 – Go Java Go! – Andres Almiray

See the table of contents for my other blog posts from the conference

@aalmiray

Java is 23 years old. Go is 9 years old

To run

  • go run prog.go – compile to temp directory and run
  • go build pro.go – explicitly compile
  • ./prog

Syntax

  • package name [no semicolons]
  • import “target”
  • fun main() {}
  • fmt.Println(“Hello World”)
  • fmt.Println(“one”, “two”)
  • if a == b [no parens]
  • func name(type int) int { return 0 }
  • fun name() (int, string) { return 0, “OK” } – can return multiple values
  • type Name struct { name int } – when need to create a type. DOn’t need a lot of them
  • s := MyStruct{value}
  • s := MyStruct{name: value}
  • fun (s MyStruct) function() {} – attaches function to a struct. Can then call on a MyStruct variable

Scope

  • uppercase symbol means public
  • lowercase symbol means private (to package)

Data structures

  • var strs = []string{“a”, “b”} or strings := []string {“a”, b”}
  • mapOfValues := make(map([string]int)) or mapOfValues := map[string]int {“a” : 1}
  • mapOfValues[“foo”] = 1
  • var array [4]int – ust be exactly 4 ints

Concepts

  • Can create nested functions
  • No classes or methods
  • Constructors are automatic
  • Get compilation error for unused code
  • Only one keyword for loops. Use “for” and omit sections to make while loop
  • No exceptions
  • float64 data type

Features unique to Go

  • The interface{} type is roughly equivalent to java.lang.Object. “Just” need to ensure provide implementation of the method.
  • can use return type “error” and call errors.New to indicate an error. Can return nil if no error. Assign error to variable named _ to indicate to compiler that intentionally ignoring. [I don’t get how this is better; it seems like there would be error handling everywhere]
  • Type cloning: type foo int – treat foo as int. However, cannot pass foo when int expected
  • Type alias: type foo = int – treat foo as int, but can pass foo when int expected
  • gRPC – use instead of REST. Remote procedure call framework. gRPC works with many languages including Java and Go.
  • web assembly – can run go code on the browser. Can compile Go code into web assembly

Performance

  • Runs faster than Java with Java defaults. Not comparing to Graal for native code
  • When build code, runs faster than simply running it because interpreted

Links

  • gobyexample.com
  • play.golang.org -run Go online
  • github.com/cdarwin/go-koans
  • golang.org/cmd/gofmt – automatically formats code according to go standards. Developers do not get to choose or argue.

My take

I learned a lot and it was easy to follow. I can feel my brain getting full.

Leave a Reply

Your email address will not be published. Required fields are marked *