本文整理汇总了Golang中io.Closer类的典型用法代码示例。如果您正苦于以下问题:Golang Closer类的具体用法?Golang Closer怎么用?Golang Closer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Closer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: closeOrErr
// closeOrErr calls cl.Close() and sets err to the returned error value if
// itself is not yet set.
func closeOrErr(cl io.Closer, err *error) {
e := cl.Close()
if *err != nil {
return
}
*err = e
}
示例2: logclose
func logclose(c io.Closer) error {
err := c.Close()
if err != nil {
logf("Error closing %T: %v", c, err)
}
return err
}
示例3: TestLockInChild
func TestLockInChild(t *testing.T) {
f := os.Getenv("TEST_LOCK_FILE")
if f == "" {
// not child
return
}
lock := Lock
if v, _ := strconv.ParseBool(os.Getenv("TEST_LOCK_PORTABLE")); v {
lock = lockPortable
}
var lk io.Closer
for scan := bufio.NewScanner(os.Stdin); scan.Scan(); {
var err error
switch scan.Text() {
case "lock":
lk, err = lock(f)
case "unlock":
err = lk.Close()
lk = nil
case "exit":
// Simulate a crash, or at least not unlocking the lock.
os.Exit(0)
default:
err = fmt.Errorf("unexpected child command %q", scan.Text())
}
if err != nil {
fmt.Println(err)
} else {
fmt.Println("")
}
}
}
示例4: Put
func (p *Pool) Put(c io.Closer) {
select {
case p.connch <- c:
default:
c.Close()
}
}
示例5: stdinWatcher
func stdinWatcher(cmd *exec.Cmd, childStdin io.Closer, graceful bool) {
b := make([]byte, 1)
for {
_, err := os.Stdin.Read(b)
if err != nil {
log.Printf("got %s when reading stdin; terminating.", err.Error())
break
}
if b[0] == '\n' || b[0] == '\r' {
log.Printf("got new line on a stdin; terminating.")
break
}
}
if graceful {
err := childStdin.Close()
if err != nil {
log.Fatalf("failed to close child's stdin: %s", err.Error())
}
} else {
err := cmd.Process.Kill()
if err != nil {
log.Fatalf("failed to kill the child: %s", err.Error())
}
os.Exit(0)
}
}
示例6: AddChild
func (c *contextGroup) AddChild(child io.Closer) {
c.children.Add(1)
go func(parent ContextGroup, child io.Closer) {
<-parent.Closing() // wait until parent is closing
child.Close() // signal child to close
parent.Children().Done() // child signals it is done
}(c, child)
}
示例7: checkClose
func checkClose(c io.Closer, err error) {
if c != nil {
cerr := c.Close()
if err == nil {
err = cerr
}
}
}
示例8: safeClose
func safeClose(obj io.Closer) (err error) {
defer func() {
if p := recover(); p != nil {
err = errors.New(fmt.Sprintf("%v", p))
}
}()
return obj.Close()
}
示例9: DoBroker
//DoBroker provides the internal copy operation
func DoBroker(dest io.WriteCloser, src io.ReadCloser, end io.Closer, err NotifierError) {
_, ex := io.Copy(dest, src)
if ex != nil {
go func() { err <- ex }()
}
end.Close()
}
示例10: closeAndExit
func closeAndExit(db io.Closer, code int, message ...interface{}) {
for _, m := range message {
fmt.Fprintln(os.Stderr, m)
}
if err := db.Close(); err != nil {
fmt.Fprintf(os.Stderr, "db.Close() error: %s\n", err)
}
os.Exit(code)
}
示例11: restoreTerminal
func (cli *DockerCli) restoreTerminal(in io.Closer) error {
if cli.state != nil {
term.RestoreTerminal(cli.inFd, cli.state)
}
if in != nil {
return in.Close()
}
return nil
}
示例12: f1
func f1() {
// The 5 here and below depends on the number of internal runtime frames
// that sit between a deferred function called during panic and
// the original frame. If that changes, this test will start failing and
// the number here will need to be updated.
defer checkLine(5)
var t *T
var c io.Closer = t
c.Close()
}
示例13: setClosed
// setClosed sets the close flag and returns the previous status.
func (s *Sniffer) setClosed(conn io.Closer) bool {
s.lock.Lock()
defer s.lock.Unlock()
old := s.closed
s.closed = true
if !old && conn != nil {
conn.Close()
}
return old
}
示例14: loggedClose
func loggedClose(c io.Closer, name string) {
err := c.Close()
if err != nil {
if name != "" {
glog.Warningf("error closing %s: %v", name, err)
} else {
glog.Warningf("error closing %v: %v", c, err)
}
}
}
示例15: wait
func (p ping) wait(duration time.Duration, done io.Closer) {
defer done.Close()
for {
select {
case <-p:
case <-time.After(duration):
return
}
}
}