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


Golang io.Reader函数代码示例

本文整理汇总了Golang中io.Reader函数的典型用法代码示例。如果您正苦于以下问题:Golang Reader函数的具体用法?Golang Reader怎么用?Golang Reader使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Reader函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Accept

// Accept starts a new SMTP session using io.ReadWriteCloser
func Accept(remoteAddress string, conn io.ReadWriteCloser, storage storage.Storage, messageChan chan *data.Message, hostname string, monkey monkey.ChaosMonkey) {
	defer conn.Close()

	proto := smtp.NewProtocol()
	proto.Hostname = hostname
	var link *linkio.Link
	reader := io.Reader(conn)
	writer := io.Writer(conn)
	if monkey != nil {
		linkSpeed := monkey.LinkSpeed()
		if linkSpeed != nil {
			link = linkio.NewLink(*linkSpeed * linkio.BytePerSecond)
			reader = link.NewLinkReader(io.Reader(conn))
			writer = link.NewLinkWriter(io.Writer(conn))
		}
	}

	session := &Session{conn, proto, storage, messageChan, remoteAddress, false, "", link, reader, writer, monkey}
	proto.LogHandler = session.logf
	proto.MessageReceivedHandler = session.acceptMessage
	proto.ValidateSenderHandler = session.validateSender
	proto.ValidateRecipientHandler = session.validateRecipient
	proto.ValidateAuthenticationHandler = session.validateAuthentication
	proto.GetAuthenticationMechanismsHandler = func() []string { return []string{"PLAIN"} }

	session.logf("Starting session")
	session.Write(proto.Start())
	for session.Read() == true {
		if monkey != nil && monkey.Disconnect != nil && monkey.Disconnect() {
			session.conn.Close()
			break
		}
	}
	session.logf("Session ended")
}
开发者ID:jackc,项目名称:MailHog-Server,代码行数:36,代码来源:session.go

示例2: send

func send(req *http.Request) (resp *http.Response, err error) {
	addr := req.URL.Host
	if !hasPort(addr) {
		addr += ":http"
	}
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		return nil, err
	}
	err = req.Write(conn)
	if err != nil {
		conn.Close()
		return nil, err
	}
	reader := bufio.NewReader(conn)
	resp, err = http.ReadResponse(reader, req)
	if err != nil {
		conn.Close()
		return nil, err
	}
	r := io.Reader(reader)
	if v := resp.Header["Content-Length"]; v != nil {
		n, err := strconv.Atoi(v[0])
		if err != nil {
			return nil, &badStringError{"invalid Content-Length", v[0]}
		}
		v := int64(n)
		r = io.LimitReader(r, v)
	}
	resp.Body = readClose{r, conn}
	return
}
开发者ID:micrypt,项目名称:gofluidinfo,代码行数:32,代码来源:fluidinfo.go

示例3: main

func main() {
	stdout := os.Stdout
	os.Stdout = os.Stderr

	executable, err := exec.LookPath("dd")
	if err != nil {
		fmt.Printf("ddp: failed to find dd: %s\n", err)
		os.Exit(1)
	}

	// Create pipe attached to a reader:
	output, input, err := os.Pipe()
	if err != nil {
		panic(err)
	}

	// Setup process with _the_ three file descriptors:
	files := []*os.File{
		os.Stdin,
		stdout,
		input,
	}
	process, err := os.StartProcess(executable, os.Args, &os.ProcAttr{
		Files: files,
	})
	if err != nil {
		fmt.Printf("ddp: failed to start dd: %s\n", err)
		os.Exit(1)
	}

	Trap(process)

	target := GuessTargetSize(os.Args)
	bar := pb.New64(target)
	bar.SetUnits(pb.U_BYTES)
	bar.ShowSpeed = true
	bar.Output = os.Stderr
	started := false

	OutputScanner(io.Reader(output), os.Stderr, func(bytes int64) {
		if !started {
			started = true
			bar.Start()
		}
		bar.Set64(bytes)
	})
	Interrupter(process, pb.DEFAULT_REFRESH_RATE)

	state, err := process.Wait()
	if err != nil {
		panic(err)
	}
	if started && state.Success() {
		bar.Finish()
	}
	output.Close()
	if !state.Success() {
		os.Exit(1)
	}
}
开发者ID:koenbollen,项目名称:ddp,代码行数:60,代码来源:main.go

示例4: InitReader

func (d *Decrypter) InitReader(fileReader io.Reader) (io.Reader, error) {
	iv := make([]byte, aes.BlockSize)
	n, err := fileReader.Read(iv)
	if err != nil {
		return io.Reader(d), err
	} else if n != aes.BlockSize {
		return io.Reader(d), fmt.Errorf("Encrypted file is too small")
	}

	decStream, err := getDecryptStream(d.passphrase, iv)
	if err != nil {
		return io.Reader(d), err
	}
	d.streamReader = &cipher.StreamReader{S: decStream, R: fileReader}
	return io.Reader(d), nil
}
开发者ID:n-boy,项目名称:backuper,代码行数:16,代码来源:crypter.go

示例5: send

func send(req *http.Request) (resp *http.Response, err os.Error) {
	addr := req.URL.Host
	if !hasPort(addr) {
		addr += ":http"
	}
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		return nil, err
	}

	err = req.Write(conn)
	if err != nil {
		conn.Close()
		return nil, err
	}

	reader := bufio.NewReader(conn)
	resp, err = http.ReadResponse(reader, req.Method)
	if err != nil {
		conn.Close()
		return nil, err
	}

	r := io.Reader(reader)
	if n := resp.ContentLength; n != -1 {
		r = io.LimitReader(r, n)
	}
	resp.Body = readClose{r, conn}

	return
}
开发者ID:rene-dev,项目名称:go-twitter,代码行数:31,代码来源:http_auth.go

示例6: getCounter

func getCounter(name string, node int64) uint64 {
	var counter uint64
	var offset int64

	fd, err := os.Open(name, os.O_RDONLY, 0666)
	if err == nil {
		defer fd.Close()
		// node 1 is the first node
		offset = (node - 1) * 8
		_, seekerr := fd.Seek(offset, 0)
		if seekerr == nil {
			ior := io.Reader(fd)
			if err := binary.Read(ior, binary.LittleEndian, &counter); err != nil {
				fmt.Printf("binary.Read failed: %s\n", err)
			} else {
				fd.Close()
				return counter
			}
		} else {
			fmt.Printf("Seek error for node %d to offset %d\n", node, offset)
			fd.Close()
		}
	} else {
		cantOpenMessageCounterFile(name)
	}

	return 0
}
开发者ID:GoogleSourceExpoted,项目名称:goesmtp,代码行数:28,代码来源:messagecounter.go

示例7: splitConfigRootFSFromTar

func splitConfigRootFSFromTar(in io.ReadCloser, config *[]byte) io.ReadCloser {
	pr, pw := io.Pipe()
	go func() {
		tarReader := tar.NewReader(in)
		tarWriter := tar.NewWriter(pw)
		defer in.Close()

		hasRootFS := false

		for {
			hdr, err := tarReader.Next()
			if err == io.EOF {
				if !hasRootFS {
					pw.CloseWithError(errors.Wrap(err, "no rootfs found"))
					return
				}
				// Signals end of archive.
				tarWriter.Close()
				pw.Close()
				return
			}
			if err != nil {
				pw.CloseWithError(errors.Wrap(err, "failed to read from tar"))
				return
			}

			content := io.Reader(tarReader)
			name := path.Clean(hdr.Name)
			if path.IsAbs(name) {
				name = name[1:]
			}
			if name == configFileName {
				dt, err := ioutil.ReadAll(content)
				if err != nil {
					pw.CloseWithError(errors.Wrapf(err, "failed to read %s", configFileName))
					return
				}
				*config = dt
			}
			if parts := strings.Split(name, "/"); len(parts) != 0 && parts[0] == rootFSFileName {
				hdr.Name = path.Clean(path.Join(parts[1:]...))
				if hdr.Typeflag == tar.TypeLink && strings.HasPrefix(strings.ToLower(hdr.Linkname), rootFSFileName+"/") {
					hdr.Linkname = hdr.Linkname[len(rootFSFileName)+1:]
				}
				if err := tarWriter.WriteHeader(hdr); err != nil {
					pw.CloseWithError(errors.Wrap(err, "error writing tar header"))
					return
				}
				if _, err := pools.Copy(tarWriter, content); err != nil {
					pw.CloseWithError(errors.Wrap(err, "error copying tar data"))
					return
				}
				hasRootFS = true
			} else {
				io.Copy(ioutil.Discard, content)
			}
		}
	}()
	return pr
}
开发者ID:docker,项目名称:docker,代码行数:60,代码来源:backend_linux.go

示例8: getQueue

func getQueue(url string, dumpFlag bool) ([]Queue, error) {
	client := NewClient(url)
	res, err := client.get("queue/api/json")
	if err != nil {
		return nil, err
	}
	defer res.Body.Close()

	if dumpFlag == true {
		buf := new(bytes.Buffer)
		buf.ReadFrom(res.Body)
		fmt.Println(buf.String())
		return nil, nil
	}

	var r struct {
		QueueItems []Queue `json:"items"`
	}
	err = json.NewDecoder(io.Reader(res.Body)).Decode(&r)
	if err != nil {
		return nil, err
	}

	return r.QueueItems, nil
}
开发者ID:hhatto,项目名称:jc,代码行数:25,代码来源:status.go

示例9: acceptUDP

func (s *Server) acceptUDP() {
	// Looping for new messages
	for {
		buf := make([]byte, MAX_MSG_SIZE)
		n, adr, err := s.udpsock.ReadFrom(buf)

		if s.comm.running {
			if err != nil {
				log.Error("Error while reading UDP (read %d) from %s: %s\n", n, adr, err)

			} else {
				abcon := net.Conn(s.udpsock)
				connection := NewConnection(s.comm.pool, P_UDP, D_Inbound, abcon)
				read := io.Reader(bytes.NewBuffer(buf))
				msg := s.comm.NewMessage()
				msg.connection = connection
				err := msg.readMessage(read)

				if err != nil {
					log.Error("Couldn't handle message received from UDP because of errors: %s %s\n", msg, err)
				} else {
					go s.comm.handleMessage(msg)
				}
			}
		} else {
			log.Info("Dropping connection because communications have been paused")
		}
	}
}
开发者ID:appaquet,项目名称:gostore,代码行数:29,代码来源:server.go

示例10: loadNames

func (t taxTree) loadNames(fname string, dict map[int]int) error {
	namesf, eopen := os.OpenFile(fname, os.O_RDONLY, 0644)
	defer namesf.Close()
	if eopen != nil {
		fmt.Fprintf(os.Stderr, "file doesn't exist %s\n", fname)
		return eopen
	}
	b := bufio.NewReader(io.Reader(namesf))
	for {
		line, _, err := b.ReadLine()
		if err == io.EOF {
			return nil
		}
		if pos := bytes.Index(line, []byte("scientific name")); pos == -1 {
			continue
		}
		gi, name, e := parsename([]byte(line)[0 : len(line)-2]) // HINT: ends in "\t|"
		if e != nil {
			return e
		}
		t[dict[gi]].Name = make([]byte, len(name))
		copy(t[dict[gi]].Name, name)
	}
	return nil
}
开发者ID:emepyc,项目名称:Blast2lca,代码行数:25,代码来源:taxonomy.go

示例11: runCat

// Run the cat command with the given arguments.
//
// The name of the stream to send data to is required. Any other arguments are
// filenames that should be sent line-by-line into Kinesis. If no files are
// passed, data is sent from Stdin.
func runCat(args []string) {
	if len(args) < 1 {
		log.Fatalln("error: no stream name given")
	}

	stream := args[0]
	inputFiles := args[1:]

	reader := io.Reader(os.Stdin)
	if len(inputFiles) > 0 {
		reader = openFiles(inputFiles)
	}
	scanner := bufio.NewScanner(reader)

	p := producer.New(stream)
	p.Debug = envBool(VERBOSE)

	for scanner.Scan() {
		line := scanner.Text()
		if len(line) > 0 {
			fatalOnErr(p.PutString(line))
		}
	}

	if err := scanner.Err(); err != nil {
		log.Fatalln("error:", err)
	}

	fatalOnErr(p.Flush())
}
开发者ID:jamtur01,项目名称:ktk,代码行数:35,代码来源:cat.go

示例12: TrainOnDir

//////GENOME TRAINING/////////////////////////////////////////////////////////////////
//Trains a genome according to the contents of a directory
func (genome *Genome) TrainOnDir(dir string) (err os.Error) {
	//open the directory
	files, err := ioutil.ReadDir(dir)
	if err != nil {
		return err
	}
	fmt.Println("analyzing", len(files), "files")
	for fileNum, fileDesc := range files {
		if fileDesc.IsRegular() {
			file, err := os.Open(strings.Join([]string{dir, fileDesc.Name}, "/"), os.O_RDONLY, 0666)
			if err != nil {
				fmt.Println("oops!!", err)
			}
			fReader := io.Reader(file)
			data, err := ioutil.ReadAll(fReader)
			genome.trainer.Train(data)
			fmt.Println("on file", fileNum)
			file.Close()
		}
	}
	genes := genome.trainer.GetGenes()
	for _, gene := range genes {
		genome.addGene(Gene(gene))
	}
	return
}
开发者ID:ericfode,项目名称:Datazome,代码行数:28,代码来源:genome.go

示例13: main

func main() {
	flag.Parse()

	if flag.NArg() != 1 {
		fmt.Fprintln(os.Stderr, "Usage: distance vectors.bin")
		os.Exit(1)
	}

	f, err := os.Open(flag.Arg(0))
	defer f.Close()
	if err != nil {
		log.Fatal(err)
	}

	embeds, err := go2vec.ReadWord2VecBinary(bufio.NewReader(io.Reader(f)), true)
	if err != nil {
		log.Fatal(err)
	}

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for scanner.Scan() {
		token := scanner.Text()
		results, err := embeds.Similarity(token, 10)
		if err != nil {
			fmt.Println(os.Stderr, err.Error())
			os.Exit(1)
		}

		for _, wordSimilarity := range results {
			fmt.Println(wordSimilarity.Word, wordSimilarity.Similarity)
		}
	}
}
开发者ID:postfix,项目名称:go2vec,代码行数:34,代码来源:distance.go

示例14: addTagsToFile

func addTagsToFile(file *os.File, tags []string) (success bool) {
	file.Seek(0, 0) // rewind to start with
	r, err := ioutil.ReadAll(io.Reader(file))
	if err != nil {
		log.Printf("ERROR: %s\n", err)
		return
	}
	loc := bytes.Index(r, gitScissorMarker)
	if loc < 0 {
		if _, err := file.Seek(0, 2); err != nil { // seek to end of file
			log.Printf("ERROR: %s\n", err)
			return
		}
	} else {
		if _, err := file.Seek(int64(loc), 0); err != nil { // seek to scissor line then truncate the file here
			log.Printf("ERROR: %s\n", err)
			return
		}
		if err := file.Truncate(int64(loc + 1)); err != nil {
			log.Printf("ERROR: %s\n", err)
			return
		}
	}
	fmt.Fprintln(file, "") // blank line first to ensure it's not stuck to the summary
	for _, tag := range tags {
		fmt.Fprintln(file, tag)
	}
	return true
}
开发者ID:GoodGuide,项目名称:gg-git-hooks,代码行数:29,代码来源:commit_msg.go

示例15: linkParser

func linkParser(page_chan chan string) <-chan string {
	link_chan := make(chan string)
	go func() {
		for page := range page_chan {
			//page := <-page_chan
			page_bytes := bytes.NewBufferString(page)
			d := html.NewTokenizer(io.Reader(page_bytes))
			for {
				tokenType := d.Next()
				if tokenType == html.ErrorToken {
					fmt.Println("\nFinished to parse page")
					break
				}
				token := d.Token()
				switch tokenType {
				case html.StartTagToken:
					if strings.EqualFold(token.Data, "A") {
						for _, a := range token.Attr {
							if strings.EqualFold(a.Key, "HREF") {
								link_chan <- a.Val
							}
						}
					}
				}
			}
		}
		close(link_chan)
	}()
	return link_chan
}
开发者ID:rodsenra,项目名称:go_exercises,代码行数:30,代码来源:main.go


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