packagemainimport("io""log""mime/multipart""net/http""os""path/filepath""runtime""github.com/cheggaaa/pb")funcmain(){iflen(os.Args)!=2{log.Println("Usage: pipeUp <filename>\n")os.Exit(1)}input,err:=os.Open(os.Args[1])check(err)deferinput.Close()stat,err:=input.Stat()check(err)pipeOut,pipeIn:=io.Pipe()bar:=pb.New(int(stat.Size())).SetUnits(pb.U_BYTES)bar.ShowSpeed=truewriter:=multipart.NewWriter(pipeIn)// do the request concurrentlyvarresp*http.Responsedone:=make(chanerror)gofunc(){// prepare requestreq,err:=http.NewRequest("POST","http://localhost:9000/upload",pipeOut)iferr!=nil{done<-errreturn}req.Header.Set("Content-Type",writer.FormDataContentType())log.Println("Created Request")bar.Start()resp,err=http.DefaultClient.Do(req)iferr!=nil{done<-errreturn}done<-nil}()part,err:=writer.CreateFormFile("file",filepath.Base(os.Args[1]))check(err)out:=io.MultiWriter(part,bar)_,err=io.Copy(out,input)check(err)check(writer.Close())check(pipeIn.Close())// need to close the pipe tocheck(<-done)bar.FinishPrint("Upload done!")}funccheck(errerror){_,file,line,_:=runtime.Caller(1)iferr!=nil{log.Fatalf("Fatal from <%s:%d>\nError:%s",file,line,err)}}