Skip to content

Commit a379536

Browse files
committed
#code commit
1 parent 927c507 commit a379536

Some content is hidden

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

58 files changed

+2707
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13+
*.idea

src/basic/MapReduce.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package basic
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
9+
}
10+
11+
func init() {
12+
fmt.Println("init method!")
13+
fmt.Println(mapReduceMethod(100))
14+
test2()
15+
}
16+
17+
func mapReduceMethod(num int) (res int) {
18+
mpChan := make(chan int, num)
19+
var result int
20+
//map
21+
for i := 1; i <= num; i++ {
22+
go func(i int) {
23+
mpChan <- i * i
24+
}(i)
25+
}
26+
for i := 0; i < num; i++ {
27+
j := <-mpChan
28+
result += j
29+
}
30+
31+
return result
32+
}
33+
34+
func test2() {
35+
36+
tch := make(chan int, 1)
37+
38+
tch <- 3
39+
40+
fmt.Println(<-tch)
41+
42+
}

src/basic/MaxRoutineTest.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package basic
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
)
7+
8+
func main() {
9+
10+
testCh := make(chan int)
11+
for i := 0; i < 1e7; i++ {
12+
go newRoutine(testCh, i)
13+
if i%100 == 0 {
14+
fmt.Println(runtime.NumGoroutine())
15+
}
16+
}
17+
18+
for i := 0; i < 1e7; i++ {
19+
20+
fmt.Println(<-testCh)
21+
}
22+
}
23+
24+
func newRoutine(ch chan int, data int) {
25+
ch <- data
26+
}

src/basic/Routine.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package basic
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
"time"
7+
)
8+
9+
func main() {
10+
const str = "hello goer!"
11+
fmt.Println(str)
12+
fmt.Println(runtime.NumCPU())
13+
go func() {
14+
fmt.Println("hello golang!")
15+
time.Sleep(time.Second)
16+
}()
17+
18+
fmt.Println(runtime.NumGoroutine())
19+
20+
var ch chan int = make(chan int, 100)
21+
22+
for i := 0; i < 100; i++ {
23+
go setData(i, ch)
24+
}
25+
26+
for i := 0; i < 100; i++ {
27+
28+
fmt.Println(<-ch)
29+
}
30+
31+
}
32+
33+
func setData(data int, ch chan int) {
34+
fmt.Println("set data ", data)
35+
ch <- data
36+
}
37+
38+
func getData(ch chan int) {
39+
fmt.Println(<-ch)
40+
}

src/basic/Routine_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package basic
2+
3+
import "testing"
4+
5+
func Test_setData(t *testing.T) {
6+
type args struct {
7+
data int
8+
ch chan int
9+
}
10+
tests := []struct {
11+
name string
12+
args args
13+
}{
14+
// TODO: Add test cases.
15+
}
16+
for _, tt := range tests {
17+
t.Run(tt.name, func(t *testing.T) {
18+
setData(tt.args.data, tt.args.ch)
19+
})
20+
}
21+
}

src/basic/RuntimeLearn.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package basic
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
)
7+
8+
func main() {
9+
10+
go serviceA()
11+
go serviceB()
12+
13+
fmt.Println("Main routine over!")
14+
var str string
15+
16+
fmt.Println("************", runtime.NumGoroutine(), "***********")
17+
fmt.Scan(&str)
18+
19+
}
20+
21+
func serviceA() {
22+
23+
for i := 0; i < 100; i++ {
24+
fmt.Println("hello world", i)
25+
runtime.Gosched()
26+
}
27+
}
28+
29+
func serviceB() {
30+
31+
for i := 0; i < 100; i++ {
32+
fmt.Println("*:", i)
33+
runtime.Gosched()
34+
}
35+
}

src/basic/basic-001.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package basic
2+
3+
import (
4+
"bufio"
5+
"errors"
6+
"fmt"
7+
"io"
8+
"log"
9+
"os"
10+
"strings"
11+
)
12+
13+
func main() {
14+
15+
rd := bufio.NewReader(os.Stdin)
16+
fmt.Println("begin getting data:")
17+
18+
for {
19+
20+
line, err := rd.ReadString('\n')
21+
if err != nil {
22+
errors.New(err.Error())
23+
}
24+
25+
if err == io.EOF {
26+
log.Print(err.Error())
27+
break
28+
}
29+
30+
if strings.Contains(line, "quit") {
31+
fmt.Println(line)
32+
break
33+
}
34+
35+
fmt.Println(line)
36+
37+
}
38+
39+
yesOrNo()
40+
41+
}
42+
43+
func yesOrNo() {
44+
45+
var str string
46+
47+
here:
48+
fmt.Println("Reading new data:")
49+
fmt.Scan(&str)
50+
51+
switch str {
52+
53+
case "yes":
54+
fmt.Println("continue")
55+
goto here
56+
case "no":
57+
fmt.Println("end here")
58+
default:
59+
goto here
60+
61+
}
62+
63+
}

src/basic/basicLearn.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (c) 2019 Jack-released.
3+
*/
4+
5+
package basic
6+
7+
import "fmt"
8+
9+
func main() {
10+
11+
var nums = [10]int{-1}
12+
13+
fmt.Print(nums)
14+
}

src/basic/testM.go

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

src/levelB-Test/basic.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2019 Jack-released.
3+
*/
4+
5+
package main
6+
7+
import "fmt"
8+
9+
func main() {
10+
11+
fmt.Println(Cnk1(4, 4))
12+
13+
}
14+
15+
func Cnk1(N int, k int) int {
16+
return Loop(N) / (Loop(k) * Loop(N-k))
17+
}
18+
19+
func Loop(n int) int {
20+
result := 1
21+
for i := 1; i <= n; i++ {
22+
result *= i
23+
}
24+
return result
25+
}

0 commit comments

Comments
 (0)