The document discusses differences between Perl and Go for Perl programmers. It covers Go topics like goroutines (threads), channels (queues), formatting code with gofmt, defining structs instead of hashes/objects, using slices instead of arrays, maps instead of hashes, error handling, importing packages instead of using Perl modules, writing tests with godoc instead of perldoc, and getting code with go get instead of cpanminus. It also provides Golang web resources for learning more.
4. Hash || Object => Struct
type Person struct {
id int // lowercase fields are not exported
email string // note no commas after variable type
Name string
HeightCentimeters float32 // camelCase convention
IsAlive bool // true or false, defaults to false
}
5. Hash || Object => Struct
var famousActor = Person{
id: 1,
email: "jeff@goldblum.org",
Name: "Jeff Goldblum", // no single quotes
HeightCentimeters: 193.5,
IsAlive: true,
}
6. Hash || Object => Struct
// we all knew this day would come
// perhaps in Independence Day: Resurgence?
// use the dot notation to make it happen
famousActor.IsAlive = false
7. Array => Slice
var tallActors []string
tallActors = append(tallActors, “Dolph Lundgren”)
tallActors[0] = “Richard Kiel”
tallActors[1] = “Chevy Chase” // error: index out of range
tallActors = append(tallActors, “Vince Vaughn”)
tallActorsCopy := make([]string, len(tallActors))
copy(tallActorsCopy, tallActors)
8. Array => Array
// not used nearly as much as slices
var tenIntegers [10]int
fiveIntegers := [5]int{1,2,3,4,5}
lastThreeIntegers := fiveIntegers[2:] // outputs 3,4,5
firstTwoIntegers := fiveIntegers[:2] // outputs 1,2
10. Loops
for i:= 0; i < 10; i++ {
fmt.Printf(“We’re going to 11”, i+1)
}
for i, actor := range []string{“Van Dam”, “Liam Neeson”} {
fmt.Printf(“#%d is %v”, i, actor) // %v default format
}
for _, actor := ... // _ ignores the loop iterator value
11. $@ => err
bytesRead, err := w.Write([]byte{“data written to client”})
if err != nil {
log.WithField("err", err).Error("write to client failed")
}
var err error // built in error type
err.Error() // format error as a string
14. t/test.t => main_test.go
main.go # file containing your code, ‘go run main.go’
main_test.go # unit test for main.go
go test # runs main_test.go, executes all tests
func TestSomething(t *testing.T) {
// do some stuff
if err != nil { t.Errorf("test failed %v", err) }
}
15. perldoc => godoc
$ godoc fmt
use 'godoc cmd/fmt' for documentation on the fmt command
PACKAGE DOCUMENTATION
package fmt
import "fmt"
...
16. perldoc => godoc
$ godoc fmt Sprintf
use 'godoc cmd/fmt' for documentation on the fmt command
func Sprintf(format string, a ...interface{}) string
Sprintf formats according to a format specifier and
returns the
resulting string.
18. cpanminus => go get
go get github.com/quipo/statsd
$ ls gocode/src/github.com/quipo/statsd/
LICENSE bufferedclient_test.go event
README.md client.go interface.go
bufferedclient.go client_test.go noopclient.go
19. PERL5LIB => GOPATH
$ echo $GOPATH
/Users/phred/gocode
$ ls gocode/
bin pkg src
$ ls gocode/src/
github.com glenda golang.org
20. ? => go build
$ pwd
~/gocode/src/myorg/myapp
$ ls
main.go main_test.go
$ go build; ls
main.go main_test.go myapp
./myapp # binary executable you can relocate to same arch
21. queues => channels
c := make(chan string) // queue for string data type
hello := “Hello SF.pm”
c <- hello
// ...
fmt.Println(<-c) // prints “Hello.SF.pm”
bufferedChannel := make(chan string, 2) // buffers 2 strings
// channels are first class citizens of Go
22. threads => goroutines
hello := “Hello SF.pm”
go myFunc(hello)
func myFunc(myString string) {
fmt.Println(myString)
}
// spawns an asynchronous thread which executes a function
// goroutines are first class citizens of Go
23. Tour of Golang web places
https://golang.org/
https://tour.golang.org/welcome/1
https://golang.org/pkg/
https://groups.google.com/forum/#!forum/golang-nuts