Go programming introduction

Post on 21-Aug-2015

55 views 1 download

Transcript of Go programming introduction

GO PROGRAMMING

GINTO JOSEPH1325919COMPUTER SCIENCE DEPTCHRIST UNIVERSITY BANGLORE

AGENDA * Introduction

* History* Versions of go* Key Words* Operators* Programs* Conclusion* References

• Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

• Go language is for google language• Developed from c & c++• General purpose language• Strongly typed language• Automatic garbage collection• Programs constructed from the packages

Introduction

History

• Introduced in 2007• Language announced in November 2009• Developed from C and C++ language • GC compiler is used to compile the program• BSD(Berkeley Software Distribution) Licensed

Creator of Go programming is Robert Griesemer, Robe Pike, And Ken Thompson

ROBERT ROBE PICK KEN THOMPSON

Key Words of go

• Break , case , chan , const , continue , default , defer , else , fallthrough , for , func , go , goto, if , import , interface , map , package , range , return , select , struct , switch , type , var

Operators

Operators combine operands into expressions.

Types of Operators1. Arithmetic Operators2. Comparison Operators3. Integer Overflow4.Logical Operators5.Address operators6.Receive operators

• Arithmetic OperatorsArithmetic operators apply to numeric values and yield a result of the same

type as the first operand. The four standard arithmetic operators (+, -, *, /) apply to integer, floating-point, and complex

types; + also applies to strings. All other arithmetic operators apply to integers only.

• + sum integers, floats, complex values, strings• - difference integers, floats, complex values • * product integers, floats, complex values • / quotient integers, floats, complex values • % remainder integers • & bitwise AND integers • | bitwise OR integers • ^ bitwise XOR integers • &^ bit clear (AND NOT) integers • << left shift integer • << unsigned integer • >> right shift integer • >> unsigned integer

• Strings can be concatenated using the + operator or the += assignment operator:

• s := "hi" • s += " and good bye“

Comparison Operators in go == equal != not equal < less <= less or equal > greater >= greater or equal

Logical operators in go && conditional AND

|| conditional OR ! NOT

Object Oriented GO

No classes , no InheritanceGo support encapsulation

Comparison between c++, java, javascript and Go

Go by Example: Hello World

package main import "fmt" func main() { fmt.Println("hello world") }

Go by Example: ValuesGo has various value types including strings, integers,

floats, booleans, etc.eg: package main

import "fmt" func main() {fmt.Println("go" + "lang")fmt.Println("1+1 =", 1+1)fmt.Println("7.0/3.0 =", 7.0/3.0)fmt.Println(true && false)fmt.Println(true || false)fmt.Println(!true)}

go by Example: Variablespackage main import "fmt" func main() {var a string = "initial" fmt.Println(a)var b, c int = 1, 2 fmt.Println(b, c)var e int fmt.Println(e)}

Go by Example: Forfor is Go’s only looping construct. Here are three basic types of for loops.

package main import "fmt" func main() { i := 1 for i <= 3 { fmt.Println(i) i = i + 1 } for j := 7; j <= 9; j++ { fmt.Println(j) } for { fmt.Println("loop") break } }

Go by Example: If/ElseBranching with if and else in Go is straight-forward.You can have an if statement without an else.

package main import "fmt“func main() {if 7%2 == 0 {fmt.Println("7 is even")} else {fmt.Println("7 is odd")}}

Go by Example: SwitchSwitch statements express conditionals across many branches.

package mainimport "fmt“func main() {i := 2switch i { case 1: fmt.Println("one") case 2: fmt.Println("two") case 3: fmt.Println("three") }}

Go by Example: ArraysIn Go, an array is a numbered sequence of elements of a specific length.package main import "fmt" func main() {var a [5]int fmt.Println("emp:", a)b := [5]int{1, 2, 3, 4, 5} fmt.Println("dcl:", b)var twoD [2][3]int

for i := 0; i < 2; i++ { for j := 0; j < 3; j++ {

twoD[i][j] = i + j}

} fmt.Println("2d: ", twoD) }

Go by Example: SlicesA slice is a segment of an arrayArrays slices are indexable and have a length

func main() {slice1 := []int{1,2,3}slice2 := append(slice1, 4, 5)fmt.Println(slice1, slice2)}

Go by Example: FunctionsFunctions are central in Go. We’ll learn about functions with a few different examples. A function is an independent section of code that maps zero or more input parameters to zero or more output parameters

package main import "fmt“func plus(a int, b int) int {return a + b}func main() {res := plus(1, 2)fmt.Println("1+2 =", res)}

Go by Example: Multiple Return ValuesGo has built-in support for multiple return values. This feature is used often in idiomatic Go, for example to return both result and error values from a function

package main import "fmt“func vals() (int, int) { return 3, 7} func main() {a, b := vals() fmt.Println(a) fmt.Println(b)_, c := vals() fmt.Println(c)}

Go by Example: PointersGo supports pointers, allowing you to pass references to values and records within your program.

package main import "fmt"func zeroval(ival int) { ival = 0}func zeroptr(iptr *int) { *iptr = 0}func main() { i := 1 fmt.Println("initial:", i) zeroval(i) fmt.Println("zeroval:", i)zeroptr(&i) fmt.Println("zeroptr:", i)fmt.Println("pointer:", &i) }

Conclusion

• Go is a fast, Small, easy to learn language• Actively developed by google• Tones of third party libraries• Learn it by using it

REFERENCES• [1] A. Ashoroft and A. Manna, 'The translation of 'go to' programs

to 'while' programs," in Proc. Int. Fed. Inform. Processing Congr., 1971, vol. 1. Amsterdam, The Netherlands: North-Holland, 1972.

• [2] S. L. Bloom and C. C. Elgot, "The existence and construction offree iterative theories," IBM Corp., Yorktown Heights, NY, Res.Rep. RC-4937, July 1974;also to appear in . Comput. Syst. Sci.

• [3] A. Bohm and A. Jacopini, "Flow diagrams, turing machines andlanguages with only two formation rules," Commun. Ass. Comput.Mach., May 1966.

• [4] A. Bruno and A. Stieglitz, "The expression of algorithms bycharts," J. Ass. Comput. Mach., July 1972.

• [5] C. C. Elgot, 'The common algebraic structure of exit automataand machines," Comput., Jan. 1971.