Skip to content

Commit

Permalink
Merge pull request #22 from hegdeashwin/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Ashwin Hegde committed Jun 27, 2015
2 parents 2544b69 + 0593fe6 commit e29f73e
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 0 deletions.
38 changes: 38 additions & 0 deletions codes/ch5_maps/check-existence-into-map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import "fmt";

func GetPrefix(name string, mustDel bool) (prefix string) {

/**
* Short hand way to declare and initialize map
*/
prefixMap := map[string] string {
"Ashwin": "Sr. Fullstack Engineer",
"Kumar": "Sr. Engineering Manager",
"Saju": "Sr. Solution Architect",
"Ajay": "Sr. Solution Architect", // comma is needed here
}

if mustDel {
delete(prefixMap, "Saju")
}

/**
* Check if the value exist into the map or not.
*/
if _, exists := prefixMap[name]; exists {
return prefixMap[name]
} else {
return "dude"
}

}

func main() {

fmt.Println("What is Saju's role? He is " + GetPrefix("Saju", false))

fmt.Println("What is Saju's role? He is " + GetPrefix("Saju", true))

}
35 changes: 35 additions & 0 deletions codes/ch6_slices/appending-to-slices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import "fmt"

func main() {

/**
* Blank [] will declare a slices.
* If you provide size like [5] will declare an array.
*/
var employee []string

/**
* Initialize a slices with type, length and capacity.
* length is required field.
*
* make([]string, length, [capacity])
*/
employee = make([]string, 3)

employee[0] = "Ashwin"
employee[1] = "Kumar"
employee[2] = "Saju"

fmt.Println("Slices: ", employee)

/**
* Appending into slices.
*/
employee = append(employee, "Ajay")
employee = append(employee, "Vinayak")

fmt.Println("After appending: ", employee)

}
23 changes: 23 additions & 0 deletions codes/ch6_slices/create-a-slices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

func main() {

/**
* Blank [] will declare a slices.
* If you provide size like [5] will declare an array.
*/
var employee []string

/**
* Initialize a slices with type, length and capacity.
* length is required field.
*
* make([]string, length, [capacity])
*/
employee = make([]string, 3)

employee[0] = "Ashwin"
employee[1] = "Kumar"
employee[2] = "Saju"
// employee[3] = "Ajay" // This will throw an error `out of range`
}
36 changes: 36 additions & 0 deletions codes/ch6_slices/delete-from-a-slices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import "fmt"

func main() {

/**
* Blank [] will declare a slices.
* If you provide size like [5] will declare an array.
*/
var employee []string

/**
* Initialize a slices with type, length and capacity.
* length is required field.
*
* make([]string, length, [capacity])
*/
employee = make([]string, 6)

employee[0] = "Ashwin"
employee[1] = "Kumar"
employee[2] = "Saju"
employee[3] = "Ajay"
employee[4] = "Vinayak"
employee[5] = "Jerin"

fmt.Println("Slices: ", employee)

// 0:2 - include 0, 1 but excludes 2
// 4: - include last
// ... - expanding slices
employee = append(employee[0:2], employee[4:]...)

fmt.Println("After Delete: ", employee)
}
12 changes: 12 additions & 0 deletions codes/ch6_slices/short-hand-way.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

func main() {

/**
* Short hand way to declare and initialize slices
*/
employee := []string {
"Ashwin", "Kumar", "Saju", "Ajay"
}

}
49 changes: 49 additions & 0 deletions codes/ch6_slices/slicing-slices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import "fmt"

func main() {

/**
* Blank [] will declare a slices.
* If you provide size like [5] will declare an array.
*/
var employee []string

/**
* Initialize a slices with type, length and capacity.
* length is required field.
*
* make([]string, length, [capacity])
*/
employee = make([]string, 7)

employee[0] = "Ashwin"
employee[1] = "Kumar"
employee[2] = "Saju"
employee[3] = "Ajay"
employee[4] = "Jerin"
employee[5] = "Vinayak"
employee[6] = "Pavan"

/**
* Slices and updating slices
*/
employee = employee[0:]
fmt.Println("[0:]", employee)

employee = employee[0:7]
fmt.Println("[0:7]", employee)

employee = employee[0:len(employee)]
fmt.Println("[0:len(employee)]", employee)

employee = employee[:4]
fmt.Println("[0:4]", employee)

employee = employee[0:4]
fmt.Println("[:4]", employee)

employee = employee[4:7]
fmt.Println("[4:7]", employee)
}
Empty file removed codes/session-6/.gitkeep
Empty file.
Empty file removed codes/session-7/.gitkeep
Empty file.
Empty file removed codes/session-8/.gitkeep
Empty file.

0 comments on commit e29f73e

Please sign in to comment.