-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd0055e
commit 6c7d6d9
Showing
14 changed files
with
669 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// func main() { | ||
// var c chan int | ||
// fmt.Printf("c=%v\n", c) | ||
// c = make(chan int, 10) | ||
// fmt.Printf("c=%v\n", c) | ||
// c <- 100 | ||
// data := <-c | ||
// fmt.Println("data:", data) | ||
// } | ||
|
||
// func produce(c chan int) { | ||
// c <- 1000 | ||
// fmt.Println("produce finished") | ||
// } | ||
// func consume(c chan int) { | ||
// data := <-c | ||
// fmt.Println("data:", data) | ||
// } | ||
// func main() { | ||
// var c chan int | ||
// fmt.Printf("c=%v\n", c) | ||
// c = make(chan int) | ||
// go produce(c) | ||
// go consume(c) | ||
// time.Sleep(time.Second * 3) | ||
// } | ||
|
||
// func hello(c chan bool){ | ||
// fmt.Println("hello goroutine") | ||
// c <- true | ||
// } | ||
// func main(){ | ||
// var exitChan chan bool | ||
// exitChan = make(chan bool) | ||
// go hello(exitChan) | ||
// fmt.Println("main thread terminate") | ||
// <-exitChan | ||
// } | ||
|
||
// func sendData(sendch chan<- int) { | ||
// sendch <- 10 | ||
// } | ||
// func readData(sendch <-chan int) { | ||
// data := <-sendch | ||
// fmt.Println("data:", data) | ||
// } | ||
// func main() { | ||
// chnl := make(chan int) | ||
// go sendData(chnl) | ||
// readData(chnl) | ||
// } | ||
|
||
// func producer(chnl chan int) { | ||
// for i := 0; i < 10; i++ { | ||
// chnl <- i | ||
// } | ||
// close(chnl) | ||
// } | ||
// func main() { | ||
// ch := make(chan int) | ||
// go producer(ch) | ||
// for { | ||
// v, ok := <-ch | ||
// if ok == false { | ||
// fmt.Println("chan is closed") | ||
// break | ||
// } | ||
// fmt.Println("Received", v, ok) | ||
// } | ||
// } | ||
|
||
// func produer(chnl chan int) { | ||
// for i := 0; i < 10; i++ { | ||
// chnl <- i | ||
// } | ||
// close(chnl) | ||
// } | ||
// func main() { | ||
// ch := make(chan int) | ||
// go produer(ch) | ||
// for v := range ch { | ||
// fmt.Println("receive:", v) | ||
// } | ||
// } | ||
|
||
// func main() { | ||
// ch := make(chan string, 2) | ||
// ch <- "hello" | ||
// ch <- "world" | ||
// s1 := <-ch | ||
// s2 := <-ch | ||
// fmt.Println(s1, s2) | ||
// } | ||
|
||
// func write(ch chan int) { | ||
// for i := 0; i < 5; i++ { | ||
// ch <- i | ||
// fmt.Println("successfully wrote", i, "to ch") | ||
// } | ||
// close(ch) | ||
// } | ||
// func main() { | ||
// ch := make(chan int, 2) | ||
// go write(ch) | ||
// time.Sleep(2 * time.Second) | ||
// for v := range ch { | ||
// fmt.Println("read value", v, "from ch") | ||
// time.Sleep(2 * time.Second) | ||
// } | ||
// } | ||
|
||
// func main() { | ||
// ch := make(chan string, 3) | ||
// ch <- "naveen" | ||
// ch <- "paul" | ||
// fmt.Println("capacity is", cap(ch)) | ||
// fmt.Println("length is", len(ch)) | ||
// fmt.Println("read value", <-ch) | ||
// fmt.Println("new length is", len(ch)) | ||
// } | ||
|
||
// func process(i int, ch chan bool) { | ||
// fmt.Println("started Goroutine ", i) | ||
// time.Sleep(2 * time.Second) | ||
// fmt.Printf("Goroutine %d ended\n", i) | ||
// ch <- true | ||
// } | ||
// func main() { | ||
// no := 3 | ||
// exitChan := make(chan bool, no) | ||
// for i := 0; i < no; i++ { | ||
// go process(i, exitChan) | ||
// } | ||
// for i := 0; i < no; i++ { | ||
// <-exitChan | ||
// } | ||
// fmt.Println("All go routines finished executing") | ||
// } | ||
|
||
func process(i int, wg *sync.WaitGroup) { | ||
fmt.Println("started Gorutine", i) | ||
time.Sleep(2 * time.Second) | ||
fmt.Printf("Goroutine %d ended\n", i) | ||
wg.Done() | ||
} | ||
func main() { | ||
no := 3 | ||
var wg sync.WaitGroup | ||
for i := 0; i < no; i++ { | ||
wg.Add(1) | ||
go process(i, &wg) | ||
} | ||
wg.Wait() | ||
fmt.Println("All goroutines finished executing") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func main() { | ||
for { | ||
var i int | ||
var curTime time.Time | ||
time.Sleep(5 * time.Second) | ||
i++ | ||
curTime = time.Now() | ||
fmt.Printf("run %d count,cur time:%v\n", i, curTime) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func isPrime(n int) bool { | ||
if n <= 1 { | ||
return false | ||
} | ||
for i := 2; i < n; i++ { | ||
if n%i == 0 { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
func produceSushu(c chan int) { | ||
var i int = 1 | ||
for { | ||
i = i + 1 | ||
result := isPrime(i) | ||
if result { | ||
c <- i | ||
} | ||
time.Sleep(time.Second) | ||
} | ||
} | ||
func consumeSushu(c chan int) { | ||
for v := range c { | ||
fmt.Printf("%D is prime\n", v) | ||
} | ||
} | ||
func main() { | ||
var intChan chan int = make(chan int, 1000) | ||
go produceSushu(intChan) | ||
go consumeSushu(intChan) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
"time" | ||
) | ||
|
||
// func hello() { | ||
// fmt.Println("hello goroutine") | ||
// } | ||
// func test() { | ||
// go hello() | ||
// fmt.Println("main thread terminate") | ||
// time.Sleep(time.Second) | ||
// } | ||
// func hello(i int) { fmt.Println("hello goroutine", i) } | ||
// func test() { | ||
// for i := 0; i < 10; i++ { | ||
// go hello(i) | ||
// } | ||
// } | ||
// func main() { | ||
// test() | ||
// time.Sleep(time.Second) | ||
// } | ||
var i int | ||
|
||
func calc() { | ||
for { | ||
i++ | ||
} | ||
} | ||
func main() { | ||
cpu := runtime.NumCPU() | ||
fmt.Println("cpu", cpu) | ||
runtime.GOMAXPROCS(1) | ||
for i := 0; i < 10; i++ { | ||
go calc() | ||
} | ||
time.Sleep(time.Hour) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[server] | ||
ip=10.238.2.2 | ||
port=8080 | ||
|
||
[mysql] | ||
username=root | ||
passwd=root | ||
database=test | ||
host=192.168.1.1 | ||
post=3838 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package iniconfig | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
func Marshal(data interface{}) (result []byte, err error) { | ||
return | ||
} | ||
func UnMarshal(data []byte, result interface{}) (err error) { | ||
|
||
linwArr := strings.Split(string(data), "\n") | ||
typeInfo := reflect.TypeOf(result) | ||
if typeInfo.Kind() != reflect.Ptr { | ||
err = errors.New("please pass address") | ||
return | ||
} | ||
typeStruct := typeInfo.Elem() | ||
if typeStruct.Kind() != reflect.Struct { | ||
err = errors.New("please pass struct") | ||
return | ||
} | ||
var lastFieldName string | ||
for index, line := range linwArr { | ||
line = strings.TrimSpace(line) | ||
if len(line) == 0 { | ||
continue | ||
} | ||
if line[0] == ';' || line[0] == '#' { | ||
return | ||
} | ||
if line[0] == '[' { | ||
lastFieldName, err = parseSection(line, typeStruct) | ||
if err != nil { | ||
err = fmt.Errorf("%v lineno:%d", err, index+1) | ||
return | ||
} | ||
continue | ||
} | ||
err = parseItem(lastFieldName, line, result) | ||
if err != nil { | ||
err = fmt.Errorf("%v lineno:%d", err, index+1) | ||
return | ||
} | ||
} | ||
return | ||
} | ||
|
||
func parseItem(lastFieldName string, line string, result interface{}) (err error) { | ||
index := strings.Index(line, "=") | ||
if index == -1 { | ||
err = fmt.Errorf("sytax error,line;%s", line) | ||
return | ||
} | ||
key := strings.TrimSpace(line[0:index]) | ||
val := strings.TrimSpace(line[index:]) | ||
if len(key) == 0 { | ||
err = fmt.Errorf("sytax error,line;%s", line) | ||
return | ||
} | ||
resultValue := reflect.ValueOf(result) | ||
sectionValue := resultValue.Elem().FieldByName(lastFieldName) | ||
sectionType := sectionValue.Type() | ||
if sectionType.Kind() != reflect.Struct { | ||
err = fmt.Errorf("field:%s must be struct", lastFieldName) | ||
return | ||
} | ||
keyFieldName := "" | ||
for i := 0; i < sectionType.NumField(); i++ { | ||
field := sectionType.Field(i) | ||
tagVal := field.Tag.Get("ini") | ||
if tagVal == key { | ||
keyFieldName = field.Name | ||
break | ||
} | ||
} | ||
if len(keyFieldName) == 0 { | ||
return | ||
} | ||
fmt.Println(keyFieldName) | ||
return | ||
} | ||
|
||
func parseSection(line string, typeInfo reflect.Type) (fieldName string, err error) { | ||
|
||
if line[0] == '[' && len(line) <= 2 { | ||
err = fmt.Errorf("syntax error,invalid section:%s", line) | ||
return | ||
} | ||
if line[0] == '[' && line[len(line)-1] != ']' { | ||
err = fmt.Errorf("syntax error,invalid sevtion:%s", line) | ||
return | ||
} | ||
if line[0] == '[' && line[len(line)-1] == ']' { | ||
sectionName := strings.TrimSpace(line[1 : len(line)+1]) | ||
if len(sectionName) == 0 { | ||
err = fmt.Errorf("syntax error,invalid sevtion:%s", line) | ||
return | ||
} | ||
for i := 0; i < typeInfo.NumField(); i++ { | ||
field := typeInfo.Field(i) | ||
tagValue := field.Tag.Get("ini") | ||
if tagValue == sectionName { | ||
fieldName = field.Name | ||
break | ||
} | ||
} | ||
} | ||
return fieldName, err | ||
} |
Oops, something went wrong.