24
Introduction to Go Language GDG DEVFEST BAGUIO - CAR 2014 UNIVERSITY OF BAGUIO TZAR C. UMANG ICT DIRECTOR – HIVE INC.

Introduction to Go language

Embed Size (px)

Citation preview

Page 1: Introduction to Go language

Introduction to

Go LanguageGDG DEVFEST BAGUIO - CAR 2014

UNIVERSITY OF BAGUIO

TZAR C. UMANG

ICT DIRECTOR – HIVE INC.

Page 2: Introduction to Go language

Flow of discussion

Requirements

Introduction

Basics

Sample Deployment

Page 3: Introduction to Go language

Requirements

Development

Software Development Kit and Test Environment

You can use Appengine’s Go SDK:

https://cloud.google.com/appengine/downloads

Python 2.7 (please don’t use the newest version)

IDE

You can use Sublimetxt and notepad++

If your on the cloud you can use Github’s online IDE

Pushing files when Deploying

Git: http://git-scm.com

Page 4: Introduction to Go language

Requirements

Development Servers

Highly recommend Google’s Appengine for Go Language

You can also use Openshift’s Go Language Environment, but you will

not have access to Appengine’s functionalities

Deployment

You can use Appengine + Cloudcompute of Google

Or Openshift Big Gear

Page 5: Introduction to Go language

Young Language

Created by: Robert Griesemer, Ken Thompson,

and Rob Pike (Googlers) in late 2007

Brought to public in 2009A new systems programming language for the

past 10 years +

It is fast to develop, compile and run

Answer to hardware’s limitations today, yet future proofing your system,

where the language handles routine and memory management

Fun and Easy making development more productive

and we don’t use semicolons “;”

Uses goroutines, a lightweight communicating processes and its

concurrent, multiplexing of it onto threads is done automatically

Has a good garbage collection, it is efficient and low in latency

Page 6: Introduction to Go language

Basics - “Hello World”

package helloworld

import (

"fmt""net/http" //http handler package

)

func init() {

http.HandleFunc("/", handle)

}

func handle(w http.ResponseWriter, r *http.Request) {fmt.Fprint(w, "<html><body><b>Hello World</b></body></html>")

}

Page 7: Introduction to Go language

Basics

Comments

Uses /**/ or //

// for line by line comment

/**/ for large chunk disabling of function or a big header credit on your code

Page 8: Introduction to Go language

Basics

Formatting

Indention is just like any other IDE for any PL tab works for them

gofmt handles alignment on you code

Parenthesis

Go uses lesser parenthesis

Structures like if, for and switch don’t require it

Operator precedence hierarchy is shorter and clearer

x<<8 + y<<16 //spaces implies what it means

Page 9: Introduction to Go language

Basics

Semicolons “ ; “

We don’t usually use it to terminate a line just like python

Mostly used to separate clauses of for – loops and the like

Making codes cleaner on the eye

Page 10: Introduction to Go language

Basics

If – statement

It looks as simple as this

if a > 1 {

return b

}

It also accept initialization statements, to setup a local variable

if err := datastore.Get(c, key, &b); err != nil {

return err

}

Page 11: Introduction to Go language

Basics

Loop

Unified for and while, there is no do-while

For

for init; condition; post { }

While

for condition { }

For(;;)

for { }

No comma operator and “+ +” and “- -” are statements not expression

// Reverse a

for i, j := 0, len(a)1; i < j; i, j = i+1, j1 {

a[i], a[j] = a[j], a[i]

}

To run multiple variables you need to use assignments

Page 12: Introduction to Go language

Basics

Switch

Go uses a more general implementation of switches

Expressions don’t need to be constants or even integers

Cases are evaluated top to bottom until a match is found

And if switch has no statement it switches to “true”

Making it possible to write if-else-if-else-if-else chain as a switch

Page 13: Introduction to Go language

Basics

Switch

func unhex(c byte) byte {

switch {

case '0' <= c && c <= '9':

return c '0'

case 'a' <= c && c <= 'f':

return c 'a' + 10

case 'A' <= c && c <= 'F':

return c 'A' + 10

}

return 0

}

Page 14: Introduction to Go language

Basics

Switch

Cases can be presented into commas as well

func runOver(c byte) bool {

switch c {

case ' ', '?', '&', '=', '#', '+', '%':

return true

}

return false

}

Page 15: Introduction to Go language

Basics

Types

It uses the regular int, float, string

Booleans (&& “and”, || “or”, ! “not”)

Explicitly sized types the likes of int8, float64

Unsigned integers, uint, uint32

Page 16: Introduction to Go language

Basics

Strings

It is built in, these are immutable values not just arrays of bytes values.

You can’t change a string variable once it is built

Though you can use snippets to change or to reassign string variables

s := "hello"

if s[1] != 'e' { os.Exit(1) }

s = "good bye"

var p *string = &s

*p = "ciao"

Page 17: Introduction to Go language

Basics

Arrays

Declared this way

var arrayOfInt [10]int;

They are like strings with values though mutable

Arrays holds values in Go making it as meaningful as a string, not like

with other language such as C where arrays are pointers

Page 18: Introduction to Go language

Basics

Pointers

We have them but they are limited

No pointer arithmetic

Easier for garbage collection

Page 19: Introduction to Go language

Basics

Database supported

MySQL, Oracle, DB2

MongoDB, NoSQL

Mobile App Delivery Supported Platform and Framework (Based on

our testing)

Intel XDK

Phonegap

Page 20: Introduction to Go language

Basics

Libraries

OS, I/O, files

math (sin(x) etc.)

strings, Unicode, regular expressions

reflection

command-line flags, logging

hashes, crypto

testing (plus testing tool, gotest)

networking, HTTP, RPC

HTML (and more general) templates

And growing…

Documentation and complete list here: http://golang.org

Page 21: Introduction to Go language

Other Resources

http://golang.org/pkg/ (package docs)

http://golang.org/src/ (source code)

Page 22: Introduction to Go language

Requirements

Development

Software Development Kit

You can use Appengine’s Go SDK:

https://cloud.google.com/appengine/downloads

Python 2.7 (please don’t use the newest version)

IDE

You can use Sublimetxt and notepad++

If your on the cloud you can use Github’s online IDE

Pushing files

Git: http://git-scm.com

Page 23: Introduction to Go language

Requirements

Development Servers

Highly recommend Google’s Appengine for Go Language

You can also Openshift’s Go Language Environment, but you will not

have access to Appengine’s functionalities

Deployment

You can use Appengine + Cloudcompute of Google

Or Openshift Big Gear

Page 24: Introduction to Go language

THANK YOU!!!

For Questions and Tutorials you may join the group

“Philippine Gophers” in FB and G+

Or add me in:

fb.com/tzarumang

twitter.com/definitelytzar

plus.google.com/tzarumang

Presentation is based from:golang.org

Chris Lupo’s The Go Programming Language