30
Go Programming Language Go-lang.ir, IRAN, March. 24, 2014 Erfan Akbarimanesh Golang Developer

Golang iran - tutorial go programming language - Preliminary

  • Upload
    go-lang

  • View
    789

  • Download
    0

Embed Size (px)

DESCRIPTION

golang,tutorial,go,golang iran,iran golang,go programming lanauge,go prg,گو لنگ,گو,زبان برنامه نویسی گو,زبان go,زبان برنامه نویسی go,زبان گولنگ,گولنگ زبان

Citation preview

Page 1: Golang iran - tutorial  go programming language - Preliminary

Go Programming Language

Go-lang.ir, IRAN, March. 24, 2014

Erfan Akbarimanesh

Golang Developer

Page 2: Golang iran - tutorial  go programming language - Preliminary

What is Go?

Go is a new programming language.

Fast compilation times

Statically-Typed Language

Non-Object Oriented But ...

Security

Open Source

Concurrent

Simple

Efficient and Productive

powerful

Page 3: Golang iran - tutorial  go programming language - Preliminary

History

Design Start in 2007

Released in 2009

Designed and Support By Google Company

Designers: Robert Griesemer, Rob Pike, Ken Thompson

Version 1.0 release in March 2012

Development continues with an active community ...

Page 4: Golang iran - tutorial  go programming language - Preliminary

Go used for?

Web applications

Server

Command-line tools

Games

Scientific computing

And etc....

Page 5: Golang iran - tutorial  go programming language - Preliminary

Which languages are similar? C language : Basic Syntax , Simple Structor

Java : Inheritance via interface , Package Definitions

C# language : Package Definitions

JavaScript : Polymorphism Independent of Inheritance

A combination of the above languages Is formed Go Programming Language

Page 6: Golang iran - tutorial  go programming language - Preliminary

Which Companies are using the Golang?

Google

Iron.io

Sound Cloud

Canonical

Heroku

Carbon Games

SmugMug

Bitly

Cloud

Page 7: Golang iran - tutorial  go programming language - Preliminary

Comparison

Faster than PHP,Python, Perl,Node.js, Ruby,...

A bit slower than C, C++ and Java (sometimes faster than Java)

See This Link For Comparison:

http://www.techempower.com/benchmarks/

...

Page 8: Golang iran - tutorial  go programming language - Preliminary

Supported OS

Linux

BSD, OpenBSD

Windows

Mac OS

Plan 9

Page 9: Golang iran - tutorial  go programming language - Preliminary

Processors Support

i386

amd64

arm

Page 10: Golang iran - tutorial  go programming language - Preliminary

Go IDE Support

IntelliJ

Sublime Text 2

LiteIDE

Intype

NetBeans

Eclipse

Zeus

and etc ...

Page 11: Golang iran - tutorial  go programming language - Preliminary

The Go Command

go command [arguments]

Commands: build compile packages and dependencies clean remove object files doc run godoc on package sources fix run go tool fix on packages fmt run gofmt on package sources get download and install packages and dependencies install compile and install packages and dependencies list list packages run compile and run Go program test test packages vet run go tool vet on packages

Example: go run hello.go

Page 12: Golang iran - tutorial  go programming language - Preliminary

Basic Application

package main

import "fmt"

func main() { fmt.Println("Golang Tutorial")

}

Page 13: Golang iran - tutorial  go programming language - Preliminary

Packages

Packages consists of one or more source file - lib (.go)

package main

Each SourceFile starts with a package

package main

import "fmt"

func main() { fmt.Println("Golang Tutorial")

}

Page 14: Golang iran - tutorial  go programming language - Preliminary

Import

Import decleration is used to express a dependency on another package:

import "fmt“

packages are imported

package main

import "fmt"

func main() { fmt.Println("Golang Tutorial")

}

Page 15: Golang iran - tutorial  go programming language - Preliminary

Comment

OneLine:

package main import "fmt" // this is a comment func main() {

fmt.Println("Hello World") }

MultiLine:

package main import "fmt" /* this is a comment this is a multiline */ func main() {

fmt.Println("Hello World") }

Page 16: Golang iran - tutorial  go programming language - Preliminary

Type

int

bool

string

int int8 int16 int32 int64

uint uint8 uint16 uint32 uint64 uintptr

byte

rune

float32 float64

complex64 complex128

Page 17: Golang iran - tutorial  go programming language - Preliminary

Type Conversion

Type Conversion in Golang Is different

package main

import "fmt"

func main(){ var x float64 = 10.5 var y int = int(x) fmt.Println(y)

}

Page 18: Golang iran - tutorial  go programming language - Preliminary

Variables

Variables can store values

var i int var f float64 = 1.5 var b bool = true var s string = "golang"

Shortcut :

i := 10 s := "Go-lang.ir" f := 1.5 b := false

Page 19: Golang iran - tutorial  go programming language - Preliminary

Constants

Constants are declared like variables, but with the const keyword. Constants can be character, string, boolean, or numeric values.

package main

import "fmt"

const Pi = 3.14

func main() { const World = "golang" fmt.Println("Hello", World) fmt.Println("Pi is:", Pi)

const Check = true fmt.Println("Check ?", Check)

}

Page 20: Golang iran - tutorial  go programming language - Preliminary

Array

Multi Value in Array

var list = […]int{1,2,3,4,5 } var list = [5]int{ 1,2,3,4,5 }

list := […]int{1,2,3,4,5 } list := [5]int{ 1,2,3,4,5 }

package main

import "fmt"

func main() { var a [2]string a[0] = "Hello" a[1] = "World" fmt.Println(a[0], a[1]) fmt.Println(a) }

Page 21: Golang iran - tutorial  go programming language - Preliminary

Slice

A slice points to an array of values and also includes a length

var list = []int{ 1, 2, 3 } var list = []string{ "foo", "bar", "zoo" }

list := []int{ 1, 2, 3 } list := []string{ "foo", "bar", "zoo" }

package main

import "fmt"

func main() { p := []int{2, 3, 5, 7, 11, 13} fmt.Println("p ==", p)

for i := 0; i < len(p); i++ { fmt.Printf("p[%d] == %d\n", i, p[i]) } }

Page 22: Golang iran - tutorial  go programming language - Preliminary

Map Non-make

M := map[string]string {}

package main import "fmt" func main(){ M := map[string]string { "x":"golang.org", "y":"go-lang.ir", } fmt.Println(M["x"],M["y"]) }

Page 23: Golang iran - tutorial  go programming language - Preliminary

Map with Make

var M map[string]string M = make(map[string]string)

package main import "fmt" var M map[string]string func main(){ M := make(map[string]string)

M = map[string]string { "x":"golang.org", "y":"go-lang.ir", } fmt.Println(M["x"],M["y"]) }

Page 24: Golang iran - tutorial  go programming language - Preliminary

Pointer

package main import "fmt" func main(){ var a int = 2 var b *int = &a a = 10 fmt.Println(a, *b) }

Page 25: Golang iran - tutorial  go programming language - Preliminary

Struct

struct is a collection of fields

type Teacher struct { Name string Family string Tell string

} package main

import "fmt"

type Teacher struct { Name string Family string Tell string

}

func main() { T := Teacher{Name: "Erfan", Family: "Akbarimanesh" , Tell : "0571"} fmt.Println(T.Name,T.Family,T.Tell) }

Page 26: Golang iran - tutorial  go programming language - Preliminary

Custom Types

type Num int type Str string type MapType map[string]int

package main import "fmt" type MapType map[string]int func main(){ M := make(MapType) M = MapType { "x":10, "y":20, } fmt.Println(M["x"],M["y"]) }

Page 27: Golang iran - tutorial  go programming language - Preliminary

Functions

package main import "fmt" func add(x int, y int) int { return x * y }

func main() { fmt.Println(add(10, 2)) }

Page 28: Golang iran - tutorial  go programming language - Preliminary

Multiple results

package main import "fmt" func Print_Value(x, y string) (string, string) { return y, x }

func main() { a, b := Print_Value("golang", ".org") fmt.Println(a, b) }

Page 29: Golang iran - tutorial  go programming language - Preliminary

Author

Erfan Akbarimanesh

Golang Developer

My Profile:

Click Here

Person Email:

[email protected]

Work EMail:

[email protected]

Page 30: Golang iran - tutorial  go programming language - Preliminary

Links

Golang English:

golang.org

Golang Persian:

go-lang.ir

Package Documentation:

golang.org/pkg

Golang Document:

golang.org/doc

Thank you