2. 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
3. Design Start in 2007
Released in 2009
Designed and Support By GoogleCompany
Designers: Robert Griesemer, Rob Pike, KenThompson
Version 1.0 release in March 2012
Development continues with an active community ...
4. Web applications
Server
Command-line tools
Games
Scientific computing
And etc....
5. 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
7. Faster than PHP,Python, Perl,Node.js, Ruby,...
A bit slower thanC, C++ and Java (sometimes faster than Java)
SeeThis Link For Comparison:
http://www.techempower.com/benchmarks/
...
11. 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
13. 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")
}
14. 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")
}
15. OneLine:
package main
import "fmt"
// this is a comment
func main() {
fmt.Println("HelloWorld")
}
MultiLine:
package main
import "fmt"
/* this is a comment
this is a multiline
*/
func main() {
fmt.Println("HelloWorld")
}
17. 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)
}
18. 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
19. 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)
}
20. MultiValue 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)
}
21. 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] == %dn", i, p[i])
}
}
22. 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"])
}
23. 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"])
}
24. package main
import "fmt"
func main(){
var a int = 2
var b *int = &a
a = 10
fmt.Println(a, *b)
}
25. struct is a collection of fields
typeTeacher struct {
Name string
Family string
Tell string
}
package main
import "fmt"
typeTeacher 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)
}
26. 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"])
}
27. package main
import "fmt"
func add(x int, y int) int {
return x * y
}
func main() {
fmt.Println(add(10, 2))
}
28. 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)
}