Skip to content

Commit 5674bba

Browse files
committed
first commit
0 parents  commit 5674bba

19 files changed

+373
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
out
2+
node_modules
3+
.vscode-test
4+
.DS_Store

Array.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var nums [10]int
7+
8+
for i := 0; i < 10; i++ {
9+
nums[i] = i + 1
10+
}
11+
PrintValue(nums[:])
12+
13+
}
14+
func PrintValue(arr []int) {
15+
for i := 0; i < len(arr); i++ {
16+
fmt.Println(arr[i], " ")
17+
}
18+
}

DecisionMaking.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
x := 10
7+
if x != 10 {
8+
fmt.Println("X is not equal to 10")
9+
} else if x == 10 {
10+
fmt.Println("X is equal to 10")
11+
}
12+
}
Binary file not shown.

ErrorHandling.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
height := 5
10+
width := 5
11+
res, err := getAreaOfARectangle(height, width)
12+
13+
if err != nil {
14+
fmt.Println(err)
15+
} else {
16+
fmt.Println(res)
17+
}
18+
19+
}
20+
func getAreaOfARectangle(height int, width int) (int, error) {
21+
if height <= 0 || width <= 0 {
22+
return 0, errors.New("Height or width is less than 1")
23+
}
24+
return (height * width), nil
25+
}

Function-as-closure.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
sum := getSum(10, 10)
7+
fmt.Println(sum())
8+
fmt.Println(sum())
9+
10+
}
11+
12+
func getSum(a, b int) func() int {
13+
i := a + b
14+
15+
return func() int {
16+
i++
17+
return i
18+
}
19+
20+
}

Function-as-value.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
sum := getSum(10, 20)
9+
fmt.Println(sum)
10+
11+
}
12+
13+
func getSum(a, b int) int {
14+
return (a + b)
15+
}

Function-call-by-reference.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
7+
var a, b uint8
8+
a = 10
9+
b = 12
10+
11+
swap(&a, &b)
12+
13+
fmt.Println("a: ", a, " ,b: ", b)
14+
}
15+
16+
func swap(a, b *uint8) {
17+
x := *a
18+
*a = *b
19+
*b = x
20+
}

Function-call-by-value.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var a, b uint8
7+
a = 10
8+
b = 12
9+
10+
x, y := swap(a, b)
11+
12+
fmt.Println("a: ", x, " ,b: ", y)
13+
}
14+
15+
func swap(a, b uint8) (uint8, uint8) {
16+
return b, a
17+
}

HelloWorld.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("Hello World")
7+
fmt.Printf("%v\n", 123.00)
8+
}

Loop.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
for i := 0; i <= 1000; i++ {
7+
if i == 850 {
8+
break
9+
}
10+
if i == 848 {
11+
continue
12+
}
13+
fmt.Println(i)
14+
}
15+
16+
}

Map.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
country := make(map[string]string)
7+
country["BD"] = "Bangladesh"
8+
country["USA"] = "America"
9+
country["IND"] = "India"
10+
11+
fmt.Println(country["BD"])
12+
fmt.Println(country)
13+
14+
delete(country, "IND")
15+
fmt.Println(country)
16+
}

Method-overriding-with-interface.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
func main() {
9+
10+
circle := Circle{5.4}
11+
fmt.Println(getArea(circle))
12+
13+
rectangle := Rectangle{10, 20}
14+
15+
fmt.Println(getArea(rectangle))
16+
}
17+
18+
type Shape interface {
19+
area() float64
20+
}
21+
22+
type Circle struct {
23+
radious float64
24+
}
25+
26+
type Rectangle struct {
27+
height, width float64
28+
}
29+
30+
func (circle Circle) area() float64 {
31+
return 3.1416 * math.Sqrt(circle.radious)
32+
}
33+
34+
func (rectangle Rectangle) area() float64 {
35+
return rectangle.height * rectangle.width
36+
}
37+
38+
func getArea(shape Shape) float64 {
39+
return shape.area()
40+
}

Range.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
7+
nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
8+
9+
for i := range nums {
10+
fmt.Print(nums[i], " ")
11+
}
12+
fmt.Println()
13+
14+
country := map[string]string{"BD": "Bangladesh", "USA": "America", "IND": "India"}
15+
16+
for code := range country {
17+
fmt.Print(code, ":", country[code], " || ")
18+
}
19+
fmt.Println()
20+
21+
for code, name := range country {
22+
fmt.Print(code, ":", name, " || ")
23+
}
24+
}

Recursion.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println(fibonacciNum(5))
7+
fmt.Println(factorial(5))
8+
}
9+
10+
func fibonacciNum(num int) int {
11+
if num <= 1 {
12+
return 1
13+
}
14+
15+
return fibonacciNum(num-1) + fibonacciNum(num-2)
16+
}
17+
18+
func factorial(num int) int {
19+
if num <= 1 {
20+
return 1
21+
}
22+
return num * factorial(num-1)
23+
24+
}

Slice.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
s := make([]int, 3)
7+
8+
// Similar to array:
9+
s[0] = 1
10+
s[1] = 2
11+
s[2] = 3
12+
fmt.Println(s)
13+
14+
fmt.Println(s[0])
15+
fmt.Println(len(s))
16+
17+
// append function is unique to slices
18+
s = append(s, 4)
19+
s = append(s, 5, 6)
20+
fmt.Println(s)
21+
22+
// slice syntax
23+
fmt.Println(s[1:3])
24+
25+
fmt.Println(s[:3])
26+
27+
fmt.Println(s[1:])
28+
29+
// concise slice definition:
30+
t := []int{100, 200, 300}
31+
fmt.Println(t)
32+
33+
//x := s
34+
//x[0] = 500
35+
//fmt.Println(x)
36+
//fmt.Println(s)
37+
38+
// Use copy to prevent from changing both x and s
39+
x := make([]int, len(s))
40+
copy(x, s)
41+
42+
x[0] = 500
43+
fmt.Println(x)
44+
fmt.Println(s)
45+
46+
// 2-D slices (similar to arrays, although lengths can vary.
47+
ss := make([][]int, 3)
48+
for i := 0; i < 3; i++ {
49+
50+
ss[i] = make([]int, 5)
51+
for j := 0; j < 5; j++ {
52+
ss[i][j] = j + 1
53+
}
54+
}
55+
fmt.Println(ss)
56+
57+
// 3-D slices (similar to arrays, although lengths can vary.
58+
sss := make([][][]int, 3)
59+
for i := 0; i < 3; i++ {
60+
61+
sss[i] = make([][]int, 6)
62+
for j := 0; j < 6; j++ {
63+
sss[i][j] = make([]int, 10)
64+
for k := 0; k < 10; k++ {
65+
sss[i][j][k] = k + 1
66+
}
67+
}
68+
}
69+
fmt.Println(sss)
70+
71+
}

Struct.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
7+
var student1 student
8+
student1.id = 1
9+
student1.name = "Shahidul"
10+
student1.address = "Kalikair,Gazipur"
11+
12+
PrintStudentInfo(student1)
13+
Modify(&student1)
14+
PrintStudentInfo(student1)
15+
}
16+
17+
type student struct {
18+
id uint8
19+
name string
20+
address string
21+
}
22+
23+
func PrintStudentInfo(stdn student) {
24+
fmt.Println(stdn)
25+
}
26+
27+
func Modify(stdn *student) {
28+
stdn.name = "zeromsi"
29+
}

Variables.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var unsignedInteger uint
7+
unsignedInteger = 255
8+
signedInteger := -255
9+
x, y, z := 1, 2, 3
10+
fmt.Println("unsignedInt: ", unsignedInteger, " signedInt: ", signedInteger, " x: ", x, " y: ", y, " z: ", z)
11+
}

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/zeromsi/go-starter
2+
3+
go 1.13

0 commit comments

Comments
 (0)