Go Programming Language
Go-lang.ir, IRAN, March. 24, 2014
Erfan Akbarimanesh
Golang Developer
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
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 ...
Go used for?
Web applications
Server
Command-line tools
Games
Scientific computing
And etc....
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
Which Companies are using the Golang?
Iron.io
Sound Cloud
Canonical
Heroku
Carbon Games
SmugMug
Bitly
Cloud
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/
...
Supported OS
Linux
BSD, OpenBSD
Windows
Mac OS
Plan 9
Processors Support
i386
amd64
arm
Go IDE Support
IntelliJ
Sublime Text 2
LiteIDE
Intype
NetBeans
Eclipse
Zeus
and etc ...
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
Basic Application
package main
import "fmt"
func main() { fmt.Println("Golang Tutorial")
}
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")
}
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")
}
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") }
Type
int
bool
string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
byte
rune
float32 float64
complex64 complex128
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)
}
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
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)
}
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) }
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]) } }
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"]) }
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"]) }
Pointer
package main import "fmt" func main(){ var a int = 2 var b *int = &a a = 10 fmt.Println(a, *b) }
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) }
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"]) }
Functions
package main import "fmt" func add(x int, y int) int { return x * y }
func main() { fmt.Println(add(10, 2)) }
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) }
Author
Erfan Akbarimanesh
Golang Developer
My Profile:
Click Here
Person Email:
Work EMail:
Links
Golang English:
golang.org
Golang Persian:
go-lang.ir
Package Documentation:
golang.org/pkg
Golang Document:
golang.org/doc
Thank you