Skip to content

Commit 75307ee

Browse files
committed
Init the repo
0 parents  commit 75307ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+643
-0
lines changed

array/slices1

1.57 MB
Binary file not shown.

array/slices1.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
import "fmt"
3+
4+
func main(){
5+
6+
var arr1 [5]int
7+
8+
for i:=0; i<len(arr1); i++ {
9+
arr1[i] = i * 2
10+
}
11+
for i:=0; i<len(arr1); i++{
12+
fmt.Printf("Array at index %d is %d\n",i,arr1[i])
13+
}
14+
}

array/slices2.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
import "fmt"
3+
4+
func main(){
5+
seasons := []string{"Spring","Summer","Autumn","Winter"}
6+
for ix,season := range seasons{
7+
fmt.Printf("season %d is: %s\n",ix,season)
8+
}
9+
10+
var season string
11+
for _,season = range seasons{
12+
fmt.Printf("%s\n",season)
13+
}
14+
}

bitoper/bitop.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main(){
6+
a := 1
7+
a = a << 1
8+
a = a << 2
9+
fmt.Println(a)
10+
}

calcproj/bin/calc

1.57 MB
Binary file not shown.

calcproj/pkg/linux_amd64/simplemath.a

1.43 KB
Binary file not shown.

calcproj/src/calc/calc.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import "os"
4+
import "fmt"
5+
import "simplemath"
6+
import "strconv"
7+
8+
var Usage = func() {
9+
fmt.Println("USAGE: calc command [arguments] ...")
10+
fmt.Println("\nThe commands are:\n\tadd\tAddition of two values.\n\tsqrt\tSquare root of a non-negative value.")
11+
}
12+
13+
func main() {
14+
args := os.Args
15+
if args == nil || len(args) < 2 {
16+
Usage()
17+
return
18+
}
19+
switch args[1] {
20+
case "add":
21+
if len(args) != 4 {
22+
fmt.Println("USAGE: calc add <integer1><integer2>")
23+
return
24+
}
25+
v1, err1 := strconv.Atoi(args[2])
26+
v2, err2 := strconv.Atoi(args[3])
27+
if err1 != nil || err2 != nil {
28+
fmt.Println("USAGE: calc add <integer1><integer2>")
29+
return
30+
}
31+
ret := simplemath.Add(v1, v2)
32+
fmt.Println("Result: ", ret)
33+
case "sqrt":
34+
if len(args) != 3 {
35+
fmt.Println("USAGE: calc sqrt <integer>")
36+
return
37+
}
38+
v, err := strconv.Atoi(args[2])
39+
if err != nil {
40+
fmt.Println("USAGE: calc sqrt <integer>")
41+
return
42+
}
43+
ret := simplemath.Sqrt(v)
44+
fmt.Println("Result: ", ret)
45+
default:
46+
Usage()
47+
}
48+
}

calcproj/src/simplemath/add.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package simplemath
2+
3+
func Add(a int, b int) int {
4+
return a + b
5+
}

calcproj/src/simplemath/add_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package simplemath
2+
3+
import "testing"
4+
5+
func TestAdd1(t *testing.T) {
6+
r := Add(1, 2)
7+
if r != 3 {
8+
t.Errorf("Add(1,2 failed. Got %d,expected 3.)", r)
9+
}
10+
}
11+
12+
func BenchmarkAdd(b *testing.B){
13+
for i:=0;i<b.N;i++{
14+
Add(1,2)
15+
}
16+
}

calcproj/src/simplemath/sqrt.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package simplemath
2+
3+
import "math"
4+
5+
func Sqrt(i int) int {
6+
v := math.Sqrt(float64(i))
7+
return int(v)
8+
}

0 commit comments

Comments
 (0)