当前位置: 首页>>代码示例>>Golang>>正文


Golang io.Closer类代码示例

本文整理汇总了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
}
开发者ID:MirkoDziadzka,项目名称:restic,代码行数:9,代码来源:repository.go

示例2: logclose

func logclose(c io.Closer) error {
	err := c.Close()
	if err != nil {
		logf("Error closing %T: %v", c, err)
	}
	return err
}
开发者ID:nilium,项目名称:dagr,代码行数:7,代码来源:log.go

示例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("")
		}
	}
}
开发者ID:cgag,项目名称:coreos-baremetal,代码行数:33,代码来源:lock_test.go

示例4: Put

func (p *Pool) Put(c io.Closer) {
	select {
	case p.connch <- c:
	default:
		c.Close()
	}
}
开发者ID:lijie,项目名称:siriusdb,代码行数:7,代码来源:pool.go

示例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)
	}
}
开发者ID:kotacc,项目名称:ns_server,代码行数:30,代码来源:main.go

示例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)
}
开发者ID:TilmanGriesel,项目名称:go-udtwrapper,代码行数:8,代码来源:ctxgroup.go

示例7: checkClose

func checkClose(c io.Closer, err error) {
	if c != nil {
		cerr := c.Close()
		if err == nil {
			err = cerr
		}
	}
}
开发者ID:elblivion,项目名称:s3gof3r,代码行数:8,代码来源:main.go

示例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()
}
开发者ID:voidException,项目名称:golucene,代码行数:8,代码来源:io.go

示例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()
}
开发者ID:influx6,项目名称:proxies,代码行数:10,代码来源:base.go

示例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)
}
开发者ID:obivan,项目名称:ord,代码行数:9,代码来源:main.go

示例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
}
开发者ID:hallyn,项目名称:docker,代码行数:9,代码来源:cli.go

示例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()
}
开发者ID:danny8002,项目名称:go,代码行数:10,代码来源:issue4388.go

示例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
}
开发者ID:mrlauer,项目名称:sniffer,代码行数:11,代码来源:sniffer.go

示例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)
		}
	}
}
开发者ID:kopeio,项目名称:imagebuilder,代码行数:10,代码来源:utils.go

示例15: wait

func (p ping) wait(duration time.Duration, done io.Closer) {
	defer done.Close()
	for {
		select {
		case <-p:
		case <-time.After(duration):
			return
		}
	}
}
开发者ID:nanobox-io,项目名称:golang-discovery,代码行数:10,代码来源:server.go


注:本文中的io.Closer类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。