Go Lang Beginner's

Workshop
Sathish VJ

Starting off ...
● Download zip file at http://github.
com/sathishvj/golang-workshops
● Contains slides and the source code
● Join our Google+ Community at http://goo.
gl/kwcxa
● Twitter hashtag: #golang
● My tutorials (oldish): http://golangtutorials.
blogspot.com
● Google Groups golang-nuts: https://groups.
google.com/forum/?fromgroups#!forum/golangnuts
● SublimeText users, try GoSublime plugin

com/p/go/downloads/list ● Extract it to a known location.org/doc/install .google. say [x]/golang ○ You should now have [x]/golang/go directory ● Set PATH to [x]/golang/go/bin ● Check version on the command line with go version ● Set GOROOT to [x]/golang/go ● Set GOPATH to [x]/golang/mygopath ● ● ○ Will be useful later (not today) when installing external packages with go get and go install Ref: http://golang.Installation ● Download zip file for your platform ○ Downloads: https://code.

golang.org/pkg ● Try go doc fmt or go doc regexp for documentation on specific packages ● If I'm slowing down for others.org ● To run documentation locally: godoc -http=:6060 Then access via browser at: http: //localhost:6060 . entertain yourself at http://tour.Documentation ● Documentation available at http: //golang.

Hello World Use any editor. Save as hw.go Run it as go run hw.1.go .

Hello World All functionality in a package. . Note that all package names are in small.println" Use dot notation to access package functions. Note capitalization of 'P' in Print. The main program has to be in 'main' package. Notice capital "P". Try what happens with "fmt. Packages of functionality are 'imported' Programs (generally) start at this function signature.

variables that sound right Functions/methods defined with "func" keyword . Small readable.Hello World No semicolons Note naming convention.

real fast execution Works equally well on multiple platforms No virtual machines and repeated VM update reminders Clean. clear.Immediately observable benefits ● ● ● ● ● ● Very fast building Compiled into native executable. concise code Familiar programming concepts .

.. ● Dependency analysis necessary for speed. ● Multi-core seen as crisis not opportunity.Go addresses . . safety. ● Types get in the way too much. ● Computers fast but software construction slow. concurrency poorly supported. ● Garbage collection.

2. Variables .

:= is definition and initialization Println automatically formats data type for printing Formatted prints with Printf Arrays and how to initialize them . etc.int i = 10 Default/Zero values. Float=0. String="".0. Int=0. C/Java .Variables Inverse of other ways of declaration.

3. Functions .

s2 string.3. Functions Function takes parameters in a similar format . You can also ignore return values with an underscore. j int) Awesomeness: Functions that are visible outside the package begin with a capital letter. i int. i.name type Combine many: (s1 string. j int) = (s1. s2 string. 10) . _ := Add(5.

go In your browser.5 Hello Web Save as hweb. go to http://localhost:8080 .go Run it as go run hweb.3.

Hello Web can also be written as import ( "fmt" "net/http" ) //on separate lines write your response into this request data handler function follows the defined function signature: fun HandleFunc(pattern string. No external server required! . handler func(ResponseWriter. *Reque nil as empty value (not null) Start built in server.

for loop .4.

(Can also give some parts).4.. Use continue to go to next loop index. . for loop For loop similar to Java. } Use break to get out of current loop.. for i:=0. { . C (No parentheses though) Infinite loop..

5. struct and object representation .

Use dot operator to access individual member variables. struct { } keywords Member variable definitions Initializing an empty struct instance. struct and object representation Use the type . Assigning values to member variables in order of struct declaration. Default/zero values assigned. Mix order or drop some variables with named variable definitions .5...

6. struct methods .

struct methods Functions 'associated' with a type .. Pointers automatically resolved . note that acc() requires a pointer to a MyCar instance. not physically in lexical scope of type. Same dot notation to reach member methods.. ...6.

7.error handling . multiple assignment.

multiple assignment. func f() (sum. value pair.error handling Package to use in converting to and from string Multiple return values should be in parentheses Named return values possible Eg. it is index and value at index For maps (hashtables). . For arrays. prod int) Accepting variables are in same order "range" keyword gives a key. it is key and value of key The same multiple return value is used in an error checking paradigm. No try-catch.

8. For arrays. value pair. prod int) Accepting variables are in same order "range" keyword gives a key. func f() (sum. it is index and value at index For maps (hashtables). it is key and value of key The same multiple return value is used in an error checking paradigm . Testing Package to use in converting to and from string Multiple return values should be in parentheses Named return values possible Eg.

8.go ● go test .go ● save file as add2_test. Testing ● create dir mymath ● save file as add2.

Test functions have to begin with Test_ Use a reference to *testing. Use functions Fatalf. Errorf to report test statuses. .8.T in all tests. Logf.go File has to end in _test. Testing add2_test. These functions will be called by the testing framework.go Import package testing to run tests.

Super fast in compilation and building.no additional server installations needed. B. Readability leads to maintainability. .. C.A recap of early benefits ● ● ● ● ● ● ● Go is simple .. In-built web server . It's clean and easily readable. Cross compilable to many platforms. orthogonal concepts. deploy on A. Z. .minimal keywords. works as it is designed. code on X. Garbage collected.

Questions Thank You Sathish VJ .