-
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
cc23971
commit 554003b
Showing
11 changed files
with
284,014 additions
and
0 deletions.
There are no files selected for viewing
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 @@ | ||
.idea/ |
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,36 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
) | ||
|
||
func main() { | ||
fset := token.NewFileSet() | ||
file, err := parser.ParseFile(fset, "../sync/pool/main.go", nil, parser.ParseComments) | ||
if err != nil { | ||
panic(err) | ||
} | ||
ast.Inspect(file, func(n ast.Node) bool { | ||
funcCall, ok := n.(*ast.CallExpr) | ||
if ok { | ||
mthd, ok := funcCall.Fun.(*ast.SelectorExpr) | ||
if ok { | ||
fmt.Printf("mthd %+v\n", mthd) | ||
id, ok := mthd.X.(*ast.Ident) | ||
if ok { | ||
if id.Name == "bufPool" { | ||
line := fset.Position(mthd.Pos()).Line | ||
fmt.Printf("line %v\n", line) | ||
for _, cg := range file.Comments { | ||
fmt.Printf("cg %v\n", cg.Text()) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return true | ||
}) | ||
} |
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,221 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"sync" | ||
) | ||
|
||
func main() { | ||
// runtime.GOMAXPROCS(2) | ||
// bytes_reader() | ||
// tee_reader() | ||
// multi_reader() | ||
multi_writer() | ||
} | ||
|
||
func bytes_reader() { | ||
f, err := os.Open("/Users/sean/Downloads/buildingapplicationonmesos.pdf") | ||
if err != nil { | ||
panic(err) | ||
} | ||
b, err := ioutil.ReadAll(f) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
r := bytes.NewReader(b) | ||
|
||
data := make([]byte, 10) | ||
_, err = r.Read(data) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Printf("data %+v\n", data) | ||
|
||
// rewind to start | ||
_, err = r.Seek(0, io.SeekStart) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
target, err := os.Create("./save") | ||
if err != nil { | ||
panic(err) | ||
} | ||
_, err = io.Copy(target, r) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func tmp_file() { | ||
|
||
f, err := ioutil.TempFile("", "upload") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer func() { | ||
n := f.Name() | ||
f.Close() | ||
os.Remove(n) | ||
}() | ||
s, err := os.Open("/Users/sean/Downloads/buildingapplicationonmesos.pdf") | ||
if err != nil { | ||
panic(err) | ||
} | ||
_, err = io.Copy(f, s) | ||
if err != nil { | ||
panic(err) | ||
} | ||
f.Seek(0, io.SeekStart) | ||
processFileMeta(f) | ||
// rewind to head | ||
f.Seek(0, io.SeekStart) | ||
handleFile(f) | ||
} | ||
|
||
func multi_reader() { | ||
f, err := os.Open("/Users/sean/Downloads/buildingapplicationonmesos.pdf") | ||
if err != nil { | ||
panic(err) | ||
} | ||
b := make([]byte, 2) | ||
_, err = f.Read(b) | ||
if err != nil { | ||
panic(err) | ||
} | ||
r := io.MultiReader(bytes.NewReader(b), f) | ||
handleFile(r) | ||
} | ||
|
||
func tee_reader() { | ||
f, err := os.Open("/Users/sean/Downloads/buildingapplicationonmesos.pdf") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// create a pipe and tee reader | ||
pr, pw := io.Pipe() | ||
tr := io.TeeReader(f, pw) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(2) | ||
|
||
go func() { | ||
defer pw.Close() | ||
defer wg.Done() | ||
handleFile(tr) | ||
}() | ||
|
||
go func() { | ||
defer wg.Done() | ||
processFileMeta(pr) | ||
}() | ||
|
||
wg.Wait() | ||
} | ||
|
||
func multi_writer() { | ||
f, err := os.Open("/Users/sean/Downloads/buildingapplicationonmesos.pdf") | ||
if err != nil { | ||
panic(err) | ||
} | ||
ar, aw := io.Pipe() | ||
br, bw := io.Pipe() | ||
cr, cw := io.Pipe() | ||
dr, dw := io.Pipe() | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(4) | ||
|
||
go func() { | ||
defer wg.Done() | ||
handleFile(ar) | ||
}() | ||
|
||
go func() { | ||
defer wg.Done() | ||
handleFile1(br) | ||
}() | ||
go func() { | ||
defer wg.Done() | ||
handleFile2(cr) | ||
}() | ||
go func() { | ||
defer wg.Done() | ||
handleFile3(dr) | ||
}() | ||
|
||
go func() { | ||
defer func() { | ||
aw.Close() | ||
bw.Close() | ||
cw.Close() | ||
dw.Close() | ||
}() | ||
|
||
mw := io.MultiWriter(aw, bw, cw, dw) | ||
io.Copy(mw, f) | ||
}() | ||
|
||
wg.Wait() | ||
|
||
} | ||
|
||
func handleFile(r io.Reader) { | ||
target, err := os.Create("./save") | ||
if err != nil { | ||
panic(err) | ||
} | ||
_, err = io.Copy(target, r) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func handleFile1(r io.Reader) { | ||
target, err := os.Create("./save1") | ||
if err != nil { | ||
panic(err) | ||
} | ||
_, err = io.Copy(target, r) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
func handleFile2(r io.Reader) { | ||
target, err := os.Create("./save2") | ||
if err != nil { | ||
panic(err) | ||
} | ||
_, err = io.Copy(target, r) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
func handleFile3(r io.Reader) { | ||
target, err := os.Create("./save3") | ||
if err != nil { | ||
panic(err) | ||
} | ||
_, err = io.Copy(target, r) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func processFileMeta(r io.Reader) { | ||
target, err := os.Create("./save_meta") | ||
if err != nil { | ||
panic(err) | ||
} | ||
_, err = io.Copy(target, r) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
Oops, something went wrong.