Skip to content

Instantly share code, notes, and snippets.

@macbaszii
Created June 6, 2015 04:23
Show Gist options
  • Save macbaszii/d505eb6f78676c845b11 to your computer and use it in GitHub Desktop.
Save macbaszii/d505eb6f78676c845b11 to your computer and use it in GitHub Desktop.
package main
import "fmt" // another package contains function
func main() {
var hello = "hello gopher!!" // type inference
// can be shorten in -> hello := "hello gopher!!"
fmt.Println(hello)
arr := [6]int{1, 2, 3, 4, 5}
// var arr2 [7]int = arr can't be compiled 'cause it is seen as different type
fmt.Println(arr)
slice := []int{1, 2, 3}
slice = append(slice, 4) // must be the same type
slice2 := append([]int{7, 8}, slice...)
fmt.Println(slice2)
// ... means use values inside as parameters
/*
interface{} looks like id, Object, ... in another language
it means any type of something
*/
profile := map[string]interface{} {
"name": "Bas",
"age": 24,
}
profile["deleting"] = 20 // can be use for editing
delete(profile, "deleting")
for i := 0; i < len(slice); i++ { // There is no ++i
fmt.Println(slice[i])
}
for _, value := range slice { // _ = index. use underscore for ignoring
fmt.Println(value)
}
// ** if write 'for x := range slice' you will get index not value
// this can be use with map and will get key, value
// infinity loop
var i = 0;
for {
if i > 10 {
break
}
i++
}
}
/*
Function in Go
- can return multiple value
- func name(n1, n2 int); n1, n2 are int
*/
func add(operand1 int, operand2 int) int {
return operand1 + operand2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment