go lang beginner's workshop

Upload: vakalakhi

Post on 02-Jun-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Go Lang Beginner's Workshop

    1/28

    Go Lang Beginner's

    WorkshopSathish VJ

  • 8/10/2019 Go Lang Beginner's Workshop

    2/28

    Download zip file at http://github.com/sathishvj/golang-workshops

    Contains slides and thesource code Join our Google+ Community at http://goo.

    gl/kwcxa Twitter hashtag: #golang My tutorials (oldish): http://golangtutorials.

    blogspot.com Google Groupsgolang-nuts: https://groups.

    google.com/forum/?fromgroups#!forum/golang-nuts

    SublimeText users, try GoSublimeplugin

    Starting off ...

    https://groups.google.com/forum/?fromgroups#!forum/golang-nutshttps://groups.google.com/forum/?fromgroups#!forum/golang-nutshttps://groups.google.com/forum/?fromgroups#!forum/golang-nutshttp://golangtutorials.blogspot.com/http://goo.gl/kwcxahttp://github.com/sathishvj/golang-workshopshttp://github.com/sathishvj/golang-workshopshttp://github.com/sathishvj/golang-workshopshttps://groups.google.com/forum/?fromgroups#!forum/golang-nutshttps://groups.google.com/forum/?fromgroups#!forum/golang-nutshttps://groups.google.com/forum/?fromgroups#!forum/golang-nutshttps://groups.google.com/forum/?fromgroups#!forum/golang-nutshttp://golangtutorials.blogspot.com/http://golangtutorials.blogspot.com/http://goo.gl/kwcxahttp://goo.gl/kwcxahttp://github.com/sathishvj/golang-workshopshttp://github.com/sathishvj/golang-workshops
  • 8/10/2019 Go Lang Beginner's Workshop

    3/28

    Download zip file for your platform Downloads: https://code.google.com/p/go/downloads/list

    Extract it to a known location, say [x]/golang You should now have [x]/golang/godirectory

    Set PATHto [x]/golang/go/bin Check version on the command line with

    go version

    Set GOROOTto [x]/golang/go

    Set GOPATHto [x]/golang/mygopath Will be useful later (not today) when installing external

    packages with go get and go install

    Ref:http://golang.org/doc/install

    Installation

    http://golang.org/doc/installhttp://golang.org/doc/installhttps://code.google.com/p/go/downloads/list
  • 8/10/2019 Go Lang Beginner's Workshop

    4/28

    Documentation

    Documentation available at http://golang.org/pkg

    Try go doc fmtor go doc regexp for

    documentation on specific packages If I'm slowing down for others, entertain

    yourself at http://tour.golang.org

    To run documentation locally:godoc -http=:6060

    Then access via browser at: http://localhost:6060

  • 8/10/2019 Go Lang Beginner's Workshop

    5/28

    1. Hello World

    Use any editor.

    Save as hw.go

    Run it as go run hw.go

  • 8/10/2019 Go Lang Beginner's Workshop

    6/28

    Hello World

    Programs (generally) start at this function signature.

    Packages of functionality are 'imported'

    All functionality in a package. The main program has to be in 'main'package.

    Use dot notation to access package functions. Note capitalization of 'P' in Print. Notethat all package names are in small.

    Notice capital "P". Try what happens with "fmt.println"

  • 8/10/2019 Go Lang Beginner's Workshop

    7/28

    Hello WorldNo semicolons

    Functions/methods defined with "func" keyword

    Note naming convention. Small readable, variables thatsound right

  • 8/10/2019 Go Lang Beginner's Workshop

    8/28

    Immediately observable benefits

    Very fast building Compiled into native executable; real fast

    execution Works equally well on multiple platforms No virtual machines and repeated VM

    update reminders Clean, clear, concise code Familiar programming concepts

  • 8/10/2019 Go Lang Beginner's Workshop

    9/28

    Go addresses ...

    Computers fast but software constructionslow.

    Dependency analysis necessary for speed,

    safety. Types get in the way too much. Garbage collection, concurrency poorly

    supported. Multi-core seen as crisis not opportunity.

  • 8/10/2019 Go Lang Beginner's Workshop

    10/28

    2. Variables

  • 8/10/2019 Go Lang Beginner's Workshop

    11/28

    Variables

    Inverse of other ways of declaration. C/Java - int i = 10

    Default/Zero values. String="", Int=0, Float=0.0, etc.

    := is definition and initialization

    Println automatically formats data type for printing

    Formatted prints with Printf

    Arrays and how to initialize them

  • 8/10/2019 Go Lang Beginner's Workshop

    12/28

    3. Functions

  • 8/10/2019 Go Lang Beginner's Workshop

    13/28

    3. Functions

    Function takes parameters in a similar format - name typeCombine many: (s1 string, s2 string, i int, j int) = (s1, s2 string, i, j int)

    Awesomeness: Functions that are visible outside the package beginwith a capital letter.

    You can also ignore return values with an underscore._ := Add(5, 10)

  • 8/10/2019 Go Lang Beginner's Workshop

    14/28

    3.5 Hello Web

    Save as hweb.go

    Run it as go run hweb.go

    In your browser, go to http://localhost:8080

  • 8/10/2019 Go Lang Beginner's Workshop

    15/28

    Hello Web

    can also be written as import ( "fmt" "net/http" ) //on separate lines

    request datawrite your response into this

    handler function follows the defined function signature: fun

    HandleFunc(pattern string, handler func(ResponseWriter, *Requ

    nil as empty value (not null)

    Start built in server. No external server required!

  • 8/10/2019 Go Lang Beginner's Workshop

    16/28

    4. for loop

  • 8/10/2019 Go Lang Beginner's Workshop

    17/28

    4. for loop

    For loop similar to Java, C(No parentheses though)

    Infinite loop.(Can also give some parts). for i:=0;; { ... }

    Use break to get out of current loop.Use continueto go to next loop index.

  • 8/10/2019 Go Lang Beginner's Workshop

    18/28

    5. struct and object representation

  • 8/10/2019 Go Lang Beginner's Workshop

    19/28

    5. struct and object representation

    Use the type ... struct { }keywords

    Member variable definitions

    Initializing an empty struct instance. Default/zero values

    assigned.

    Assigning values to member variables in order of structdeclaration.

    Use dot operator to access individual member variables.

    Mix order or drop some variables with named variabledefinitions

  • 8/10/2019 Go Lang Beginner's Workshop

    20/28

    6. struct methods

  • 8/10/2019 Go Lang Beginner's Workshop

    21/28

    6. struct methods

    Same dot notation to reach member methods.

    Functions 'associated' with a type ... not physically inlexical scope of type.

    Pointers automatically resolved ... note that acc()requires a pointer to aMyCar instance.

  • 8/10/2019 Go Lang Beginner's Workshop

    22/28

    7. multiple assignment,error handling

  • 8/10/2019 Go Lang Beginner's Workshop

    23/28

    multiple assignment,error handling

    Package to use in converting to and from string

    Multiple return values should be in parentheses

    Accepting variables are in same order

    The same multiple return value is used in an errorchecking paradigm. No try-catch.

    "range" keyword gives a key, valuepair.For arrays, it is indexand value at indexFor maps (hashtables), it is key and value of key

    Named return values possible

    Eg. func f() (sum, prod int)

  • 8/10/2019 Go Lang Beginner's Workshop

    24/28

    8. Testing

    Package to use in converting to and from string

    Multiple return values should be in parentheses

    Accepting variables are in same order

    The same multiple return value is used in an error

    checking paradigm

    "range" keyword gives a key, valuepair.For arrays, it is indexand value at indexFor maps (hashtables), it is key and value of key

    Named return values possible

    Eg. func f() (sum, prod int)

  • 8/10/2019 Go Lang Beginner's Workshop

    25/28

    8. Testing

    create dir

    mymath save file as

    add2.go

    save file asadd2_test.go

    go test

  • 8/10/2019 Go Lang Beginner's Workshop

    26/28

    8. Testing

    Import package testingto run tests.

    add2_test.go File has to end in_test.go

    Test functions have to begin with Test_

    Use a reference to *testing.Tin all tests. These

    functions will be called by the testing framework.

    Use functions Fatalf, Logf, Errorfto report teststatuses.

  • 8/10/2019 Go Lang Beginner's Workshop

    27/28

    Go is simple - minimal keywords, orthogonalconcepts, works as it is designed.

    It's clean and easily readable. Readability leads to maintainability. Cross compilable to many platforms; code on X,

    deploy on A, B, C, ... Z. Super fast in compilation and building.

    Garbage collected. In-built web server - no additional server

    installations needed.

    A recap of early benefits

  • 8/10/2019 Go Lang Beginner's Workshop

    28/28

    Thank YouSathish VJ

    Questions