當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。