Skip to content

variables related stuff #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions 03_variables/02_var_zero-value-abk/test-abk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import "fmt"


// var keyword
var y = 9.99

func main() {
// Short delcation operator
x := 44.5

fmt.Println(x)

foo()
}

func foo() {
fmt.Println("in foo",y)
}

43 changes: 43 additions & 0 deletions 03_variables/04_exercise_solutions/ninja-level1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import "fmt"

var x int = 42
var y string = "James Bond"
var z bool = true

type abk_int int
type hotdog int

var x_myint abk_int

func main() {

// exercise3()
// exercise4()
exercise5()

}

func exercise3() {

s := fmt.Sprintf("%d %s %t",x,y,z)
fmt.Println(s)
}

func exercise4() {
fmt.Printf("%d %T\n",x_myint,x_myint)
x_myint = abk_int(100)

fmt.Printf("%d %T\n",x_myint,x_myint)
}

func exercise5() {
fmt.Printf("%d %T\n",x_myint,x_myint)
x_myint = abk_int(100)
fmt.Printf("%d %T\n",x_myint,x_myint)

x = int(x_myint)
fmt.Printf("%d %T\n",x, x)

}
111 changes: 111 additions & 0 deletions 20_struct/10_exercises/ninja-level5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main

import (
"fmt"
)

type person struct {
fname string
lname string
ficf []string
}

func main() {

fmt.Println("\nSolution 2\n")
solution2()

fmt.Println("\nSolution 3\n")
solution3()

fmt.Println("\nSolution 4\n")
solution4()
}

func solution2() {

//Creation of maps using structs
p1 := person{
fname: "aj",
lname: "kul",
ficf: []string{"kasata", "vanilla"},
}
p2 := person{
fname: "Ro",
lname: "kul1",
ficf: []string{"rainbow", "mint", "chocolate"},
}

/*
fmt.Println(p1)
for _, icecream := range p1.ficf {
fmt.Println(icecream)
}
for _, icecream := range p2.ficf {
fmt.Println(icecream)
}
*/

m := map[string]person{
p1.lname: p1,
p2.lname: p2,
}

fmt.Println(m["kul"])
}


type vehicle struct {
doors int
color string
}

type truck struct {
vehicle
fourWheel bool
}

type sedan struct {
vehicle
luxury bool
}

func solution3() {

// Usage of embedded structs
t1 := truck{
vehicle: vehicle{
doors: 2,
color: "red"},
fourWheel: true,
}

s1 := sedan{
vehicle: vehicle{
doors: 4,
color: "black",
},
luxury: false,
}

fmt.Println(t1)
fmt.Println(s1)
fmt.Printf("%d %s %t", t1.doors, t1.color, t1.fourWheel)


}



func solution4() {

// Usage of anonymous struct
t1 := struct {
doors int
color string
}{
doors: 4,
color: "red",
}
fmt.Println(t1)
}
122 changes: 122 additions & 0 deletions 21_interfaces/06_exercises/ninja-level6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package main

import (
"fmt"
"math"
)

func main() {
solution3()
solution4()
solution5()
solution6()
solution7()
fmt.Println(solution8()())
}

func solution3() {

y := []int{10, 200, 30}
z := []int{10, 200, 300}


defer fmt.Println(foo(y...))
fmt.Println(bar(z))
}

func foo(x ...int) int {

sum := 0
for _, num := range x {
sum += num
}
return sum
}

func bar(x []int) int {
sum := 0
for _, num := range x {
sum += num
}
return sum
}


//Solution 4

type person struct {
first string
last string
age int
}

func (p person) speak() {
fmt.Printf("\n name: %s %s age: %d", p.first, p.last, p.age)

}

func solution4() {

p1 := person{
"abk",
"ku",
41,
}
p1.speak()
}


type Square struct {
length float64
width float64
}

type Circle struct {
radius float64
}

type Shape interface {
area() float64
}

func info(s Shape) {
fmt.Println(s.area())
}

func (s Square) area() float64 {
return (s.length * s.width)
}

func (c Circle) area() float64 {
return (math.Pi * c.radius * c.radius)
}

func solution5() {

s1 := Square{10, 10}
c1 := Circle{9}
info(s1)
info(c1)
}


func solution6() {

func(x int) {
fmt.Println("I am anonymous function", x)
}(10)
}

func solution7() {
f1 := func() {
fmt.Println("Function Assignment ")
}
f1()
}

func solution8() func() int {

return func() int {
return 42
}
}