Skip to content

Commit

Permalink
updat
Browse files Browse the repository at this point in the history
  • Loading branch information
xujihui1985 committed Mar 28, 2021
1 parent cc23971 commit 554003b
Show file tree
Hide file tree
Showing 11 changed files with 284,014 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
36 changes: 36 additions & 0 deletions ast/main.go
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 added ioreader/.DS_Store
Binary file not shown.
221 changes: 221 additions & 0 deletions ioreader/main.go
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)
}
}
Loading

0 comments on commit 554003b

Please sign in to comment.