Golang NewScanner示例

说明

golang newscanner示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Golang

命名空间/包名称: bufio

示例#1
文件: problem_011.go 项目: katamaritaco/ProjectEulerPracticeProblems

func populateGridFromStdin() [GRID_ROW_SIZE][GRID_COL_SIZE]int {

	var grid [GRID_ROW_SIZE][GRID_COL_SIZE]int

	i := 0

	inputScanner := bufio.NewScanner(os.Stdin)

	for inputScanner.Scan() {

		j := 0

		lineScanner := bufio.NewScanner(strings.NewReader(inputScanner.Text()))
		lineScanner.Split(bufio.ScanWords)

		for lineScanner.Scan() {

			grid[i][j], _ = strconv.Atoi(lineScanner.Text())
			j++

		}

		if j == 0 {
			break
		}

		i++
	}

	return grid
}

示例#2
文件: apib.go 项目: go-gophers/gophers

// RecordResponse writes response's status, headers and body.
func (r *APIB) RecordResponse(resp *http.Response, status, headers, body []byte, wc io.WriteCloser) (err error) {
	indent := strings.Repeat(" ", 12)

	// indent headers
	var headersS []string
	s := bufio.NewScanner(bytes.NewReader(headers))
	for s.Scan() {
		headersS = append(headersS, indent+s.Text())
	}
	if err = s.Err(); err != nil {
		return
	}

	// indent body
	var bodyS []string
	s = bufio.NewScanner(bytes.NewReader(body))
	for s.Scan() {
		bodyS = append(bodyS, indent+s.Text())
	}
	if err = s.Err(); err != nil {
		return
	}

	err = apibResponseTemplate.Execute(wc, map[string]interface{}{
		"StatusCode": resp.StatusCode,
		"Headers":    strings.Join(headersS, "\n"),
		"Body":       strings.Join(bodyS, "\n"),
	})
	if err == nil {
		err = wc.Close()
	}
	return
}

示例#3
文件: main.go 项目: Pandaman/natural-language-processing100-practice

func MergeColumns() {
	f1, err := os.Open(COL1_FILENAME)
	if err != nil {
		log.Fatalln("open col1.txt error:", err)
	}
	defer f1.Close()
	s1 := bufio.NewScanner(f1)
	f2, err := os.Open(COL2_FILENAME)
	if err != nil {
		log.Fatalln("open col2.txt error:", err)
	}
	defer f2.Close()
	s2 := bufio.NewScanner(f2)

	mf, err := os.Create(MERGED_FILENAME)
	if err != nil {
		log.Fatalln("create merged.txt error:", err)
	}
	defer mf.Close()

	for s1.Scan() && s2.Scan() {
		c1 := s1.Text()
		c2 := s2.Text()
		fmt.Fprintf(mf, "%s\t%s\n", c1, c2)
	}
}

示例#4
文件: hooks.go 项目: layer-x/layerx-commons

func LogCommand(cmd *exec.Cmd, asDebug bool) {
	logrus.WithField("command", cmd.Args).Debugf("running command")
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		return
	}
	stderr, err := cmd.StderrPipe()
	if err != nil {
		return
	}
	go func() {
		in := bufio.NewScanner(stdout)
		for in.Scan() {
			if asDebug {
				logrus.Debugf(in.Text())
			} else {
				logrus.Infof(in.Text())
			}
		}
	}()
	go func() {
		in := bufio.NewScanner(stderr)
		for in.Scan() {
			logrus.Debugf(in.Text())
		}
	}()
}

示例#5
文件: main.go 项目: peterbourgon/training

func runClient(addr string) {
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		log.Fatal(err)
	}

	defer conn.Close()

	// Messages from the conn to stdout.
	// The scanner will exit when the conn is closed.
	// We close the conn via the defer.
	go func() {
		s := bufio.NewScanner(conn)
		for s.Scan() {
			fmt.Fprintf(os.Stdout, s.Text()+"\n")
		}
	}()

	// Messages from stdin to the conn.
	// The scanner will exit when stdin is closed.
	// We close stdin via ctrl-D.
	s := bufio.NewScanner(os.Stdin)
	for s.Scan() {
		fmt.Fprintf(conn, s.Text()+"\n")
	}
}

示例#6
文件: main.go 项目: ericcapricorn/gotags

func readNames(names []string) ([]string, error) {
	if len(inputFile) == 0 {
		return names, nil
	}

	var scanner *bufio.Scanner
	if inputFile != "-" {
		in, err := os.Open(inputFile)
		if err != nil {
			return nil, err
		}

		defer in.Close()
		scanner = bufio.NewScanner(in)
	} else {
		scanner = bufio.NewScanner(os.Stdin)
	}

	for scanner.Scan() {
		names = append(names, scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		return nil, err
	}

	return names, nil
}

示例#7
文件: sirc.go 项目: qeedquan/misc_utilities

func (c *Client) listen() {
	r := bufio.NewScanner(c)
	res := time.Now()
	for {
		select {
		case <-c.quit:
			c.Close()
			return

		default:
			c.SetReadDeadline(time.Now().Add(120 * time.Second))
			if !r.Scan() {
				if ne, ok := r.Err().(net.Error); ok {
					if ne.Temporary() && ne.Timeout() && time.Since(res) < 300*time.Second {
						r = bufio.NewScanner(c)
						c.sout("PING %s", c.Host)
						continue
					}
				}
				c.err <- r.Err()
				return
			}
			c.psrv(r.Text())
			res = time.Now()
		}
	}
}

示例#8
文件: stemmer_test.go 项目: suhailpatel/go-stemmer

// I actually wanted to have a test containing all the words used in the
// original paper but Martin Porter (author of the original algorithm)
// has provided a massive collection of ~23000 words with the expected
// output of the algorithm so we can test our implementation against
// the original paper implementation
func TestCorpus(t *testing.T) {
	input, errIn := os.Open("corpus/test_input.txt")
	output, errOut := os.Open("corpus/test_output.txt")

	defer input.Close()
	defer output.Close()

	if errIn != nil || errOut != nil {
		t.Fatalf("Could not read input or output test files [%s, %s]", errIn, errOut)
	}

	inScan := bufio.NewScanner(input)
	outScan := bufio.NewScanner(output)

	for inScan.Scan() && outScan.Scan() {
		in := inScan.Text()
		out := outScan.Text()
		stemmed := Stem(in)

		t.Logf("[PASS] Input: %s → Expected: %s, Stemmed: %s\n", in, out, stemmed)

		if !strings.EqualFold(out, stemmed) {
			t.Errorf("[FAIL] Expected %s but got %s for input %s\n", out, stemmed, in)
		}
	}

	if inScan.Err() != nil || outScan.Err() != nil {
		t.Fatalf("Could not open scanner for input or output test files [%s, %s]", inScan.Err(), outScan.Err())
	}
}

示例#9
文件: example_test.go 项目: billyboar/GCSolutions

// Secure chat client
func ExampleSecureConn_client() error {
	// connect to remote server
	c, err := net.Dial("tcp", "localhost:8080")
	if err != nil {
		return err
	}
	defer c.Close()

	// wrap insecure client, perform key exchange and retrieve
	// a secured connection
	sc, err := secureconn.New(c)
	if err != nil {
		return err
	}
	defer sc.Close()

	// read messages from the server line by line
	go func() {
		scanner := bufio.NewScanner(sc)
		for scanner.Scan() {
			fmt.Println(scanner.Text())
		}
	}()

	// read messages from command line to send to server
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		_, err := sc.Write([]byte(scanner.Text() + "\n"))
		if err != nil {
			return err
		}
	}

	return nil
}

示例#10
文件: utils.go 项目: ksmaheshkumar/stolon

func (tm *TestKeeper) Start() error {
	if tm.cmd != nil {
		panic(fmt.Errorf("tm: %s, cmd not cleanly stopped", tm.id))
	}
	tm.cmd = exec.Command(tm.keeperBin, tm.args...)
	stdoutPipe, err := tm.cmd.StdoutPipe()
	if err != nil {
		return err
	}
	go func() {
		scanner := bufio.NewScanner(stdoutPipe)
		for scanner.Scan() {
			fmt.Printf("[%s]: %s\n", tm.id, scanner.Text())
		}
	}()

	stderrPipe, err := tm.cmd.StderrPipe()
	if err != nil {
		return err
	}
	go func() {
		scanner := bufio.NewScanner(stderrPipe)
		for scanner.Scan() {
			fmt.Printf("[%s]: %s\n", tm.id, scanner.Text())
		}
	}()
	err = tm.cmd.Start()
	if err != nil {
		return err
	}
	return nil
}

示例#11
文件: unitrie.go 项目: postfix/unitrie

// Creates new Trie struct loaded from filename
func Load(filename string, maxLength int) (*Trie, error) {
	file, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	cfs := make(map[rune]int)
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		w := scanner.Text()
		if maxLength < 1 || len(w) < maxLength {
			CharFreqs(&cfs, w)
		}
	}

	t := CustomNew(cfs)

	if _, err := file.Seek(0, 0); err != nil {
		return nil, err
	}

	scanner = bufio.NewScanner(file)
	for scanner.Scan() {
		w := scanner.Text()
		if maxLength < 1 || len(w) < maxLength {
			t.AddWord(w)
		}
	}

	return t, nil
}

示例#12
文件: utils.go 项目: ksmaheshkumar/stolon

func (ts *TestSentinel) Start() error {
	if ts.cmd != nil {
		panic(fmt.Errorf("ts: %s, cmd not cleanly stopped", ts.id))
	}
	ts.cmd = exec.Command(ts.sentinelBin, ts.args...)
	stdoutPipe, err := ts.cmd.StdoutPipe()
	if err != nil {
		return err
	}
	go func() {
		scanner := bufio.NewScanner(stdoutPipe)
		for scanner.Scan() {
			fmt.Printf("[%s]: %s\n", ts.id, scanner.Text())
		}
	}()

	stderrPipe, err := ts.cmd.StderrPipe()
	if err != nil {
		return err
	}
	go func() {
		scanner := bufio.NewScanner(stderrPipe)
		for scanner.Scan() {
			fmt.Printf("[%s]: %s\n", ts.id, scanner.Text())
		}
	}()
	err = ts.cmd.Start()
	if err != nil {
		return err
	}
	return nil
}

示例#13
文件: executor.go 项目: xsb/dog

func gatherCmdOutput(taskName string, cmd *exec.Cmd, eventsChan chan *types.Event) error {
	stdoutReader, err := cmd.StdoutPipe()
	if err != nil {
		return err
	}

	stderrReader, err := cmd.StderrPipe()
	if err != nil {
		return err
	}

	stdoutScanner := bufio.NewScanner(stdoutReader)
	stderrScanner := bufio.NewScanner(stderrReader)
	go func() {
		for stdoutScanner.Scan() {
			eventsChan <- types.NewOutputEvent(taskName, stdoutScanner.Bytes())
		}
	}()

	go func() {
		for stderrScanner.Scan() {
			eventsChan <- types.NewOutputEvent(taskName, stderrScanner.Bytes())
		}
	}()

	return nil
}

示例#14
文件: Piping_Pipe.go 项目: suntong/lang

func main() {
	// EXAMPLE 1
	reader := StringPump("Test")

	scanner := bufio.NewScanner(reader)
	for scanner.Scan() {
		fmt.Printf("Received: %s \n", scanner.Text())
	}

	// EXAMPLE 2
	reader = Capitalize(StringPump("Test"))

	scanner = bufio.NewScanner(reader)
	for scanner.Scan() {
		fmt.Printf("Received: %s \n", scanner.Text())
	}

	// EXAMPLE 3
	ToLower := WrapStringWordFilter(strings.ToLower)
	reader = ToLower(Capitalize(StringPump("Test THIS bit")))

	scanner = bufio.NewScanner(reader)
	for scanner.Scan() {
		fmt.Printf("Received: %s \n", scanner.Text())
	}

	fmt.Printf("Finished.\n")
}

示例#15
文件: logging.go 项目: pselibas/semaphore

func (t *task) logCmd(cmd *exec.Cmd) {
	stderr, _ := cmd.StderrPipe()
	stdout, _ := cmd.StdoutPipe()

	go t.logPipe(bufio.NewScanner(stderr))
	go t.logPipe(bufio.NewScanner(stdout))
}

示例#16
文件: main.go 项目: dbenque/maglogparser

func scannerFromFile(reader io.Reader) (*bufio.Scanner, error) {

	var scanner *bufio.Scanner
	//create a bufio.Reader so we can 'peek' at the first few bytes
	bReader := bufio.NewReader(reader)

	testBytes, err := bReader.Peek(64) //read a few bytes without consuming
	if err != nil {
		return nil, err
	}
	//Detect if the content is gzipped
	contentType := http.DetectContentType(testBytes)

	fmt.Printf("Content Type:%s\n", contentType)

	//If we detect gzip, then make a gzip reader, then wrap it in a scanner
	if strings.Contains(contentType, "x-gzip") {
		gzipReader, err := gzip.NewReader(bReader)
		if err != nil {
			return nil, err
		}

		scanner = bufio.NewScanner(gzipReader)

	} else {
		//Not gzipped, just make a scanner based on the reader
		scanner = bufio.NewScanner(bReader)
	}

	return scanner, nil
}

示例#17
文件: beaglebone_adaptor.go 项目: ympons/gobot

func ensureSlot(slots, item string) (err error) {
	fi, err := sysfs.OpenFile(slots, os.O_RDWR|os.O_APPEND, 0666)
	defer fi.Close()
	if err != nil {
		return
	}

	// ensure the slot is not already written into the capemanager
	// (from: https://github.com/mrmorphic/hwio/blob/master/module_bb_pwm.go#L190)
	scanner := bufio.NewScanner(fi)
	for scanner.Scan() {
		line := scanner.Text()
		if strings.Index(line, item) > 0 {
			return
		}
	}

	_, err = fi.WriteString(item)
	if err != nil {
		return err
	}
	fi.Sync()

	scanner = bufio.NewScanner(fi)
	for scanner.Scan() {
		line := scanner.Text()
		if strings.Index(line, item) > 0 {
			return
		}
	}
	return
}

示例#18
文件: import.go 项目: unrealord/bitcannon

func importReader(reader io.Reader, gzipped bool) {
	var scanner *bufio.Scanner
	if gzipped {
		gReader, err := gzip.NewReader(reader)
		if err != nil {
			log.Println("[ERR] My bad! I tried to start uncompressing your archive but failed.")
			log.Println("      Try checking the file, or send me the file so I can check it out.")
			return
		}
		defer gReader.Close()
		log.Println("[OK!] GZip detected, unzipping enabled")
		scanner = bufio.NewScanner(gReader)
	} else {
		scanner = bufio.NewScanner(reader)
	}
	log.Println("[OK!] Reading initialized")
	imported := 0
	skipped := 0
	// Now we scan ୧༼ಠ益ಠ༽୨
	for scanner.Scan() {
		status, _ := importLine(scanner.Text())
		if status {
			imported++
		} else {
			skipped++
		}
	}
	log.Println("[OK!] Reading completed")
	log.Println("      " + strconv.Itoa(imported) + " torrents imported")
	log.Println("      " + strconv.Itoa(skipped) + " torrents skipped")
}

示例#19
文件: annotate_trace.go 项目: strickyak/doing_os9

func main() {
	d := make(map[string]string)
	for _, a := range os.Args[1:] {
		ww := strings.Split(a, ",")
		tag, fname := ww[0], ww[1]
		println(a)
		r, err := os.Open(fname)
		if err != nil {
			panic(fname)
		}
		sc := bufio.NewScanner(r)
		for sc.Scan() {
			line := sc.Text()
			GrokLine(line, tag, d)
		}

		r.Close()
	}

	r := os.Stdin
	sc := bufio.NewScanner(r)
	for sc.Scan() {
		line := sc.Text()
		tail := ""
		ww := strings.Split(line, " ")
		if len(ww) > 0 && len(ww[0]) > 0 {
			k := strings.ToUpper(ww[0])
			if v, ok := d[k]; ok {
				tail = ReplaceWhite(v, " ")
			}
		}
		Println(line + " ;; " + tail)
	}
}

示例#20
文件: shell.go 项目: tmknom/aws-infrastructure

func (s Shell) executeCommand() (err error) {
	outReader, err := s.Command.StdoutPipe()
	if err != nil {
		return
	}

	errReader, err := s.Command.StderrPipe()
	if err != nil {
		return
	}

	var bufout, buferr bytes.Buffer
	outReader2 := io.TeeReader(outReader, &bufout)
	errReader2 := io.TeeReader(errReader, &buferr)

	s.Command.Start()
	outScanner := bufio.NewScanner(outReader2)
	for outScanner.Scan() {
		fmt.Println(outScanner.Text())
	}

	errScanner := bufio.NewScanner(errReader2)
	for errScanner.Scan() {
		fmt.Println("[stderr]" + errScanner.Text())
	}

	s.Command.Wait()
	return
}

示例#21
文件: stream.go 项目: samuell/experiments

func main() {
	exb := make([]byte, 0, 128*128)
	buf := bytes.NewBuffer(exb)

	f, err := os.Open("tmp.txt")
	if err != nil {
		panic(err)
	}
	s := bufio.NewScanner(f)
	start := make(chan int)
	go func() {
		<-start
		for s.Scan() {
			if s.Err() != nil {
				panic(err)
			}
			_, err := buf.WriteString(s.Text() + "\n")
			if err != nil {
				panic(err)
			}
		}
	}()

	start <- 1
	s2 := bufio.NewScanner(buf)
	for s2.Scan() {
		if err := s2.Err(); err != nil {
			panic(err)
		}
		fmt.Println("Received line:", s2.Text())
	}
}

示例#22
文件: plugin.go 项目: gwmoura/yati

func (lbe *Executor) Start() (*bufio.Scanner, *bufio.Scanner, error) {
	var err error

	log.Debugf("Launching plugin server for driver %s", lbe.DriverName)

	cmd := exec.Command(lbe.binaryPath)

	lbe.pluginStdout, err = cmd.StdoutPipe()
	if err != nil {
		return nil, nil, fmt.Errorf("Error getting cmd stdout pipe: %s", err)
	}

	lbe.pluginStderr, err = cmd.StderrPipe()
	if err != nil {
		return nil, nil, fmt.Errorf("Error getting cmd stderr pipe: %s", err)
	}

	outScanner := bufio.NewScanner(lbe.pluginStdout)
	errScanner := bufio.NewScanner(lbe.pluginStderr)

	os.Setenv(PluginEnvKey, PluginEnvVal)
	os.Setenv(PluginEnvDriverName, lbe.DriverName)

	if err := cmd.Start(); err != nil {
		return nil, nil, fmt.Errorf("Error starting plugin binary: %s", err)
	}

	return outScanner, errScanner, nil
}

示例#23
文件: scan_ssh.go 项目: xiaoxiaoleo/crack_ssh

// read lime from file and Scan
func Prepare(iplist, user_dict, pass_dict string) (slice_iplist, slice_user, slice_pass []string) {
	iplistFile, _ := os.Open(iplist)
	defer iplistFile.Close()
	scanner := bufio.NewScanner(iplistFile)
	scanner.Split(bufio.ScanLines)
	for scanner.Scan() {
		slice_iplist = append(slice_iplist, scanner.Text())
	}

	user_dictFile, _ := os.Open(user_dict)
	defer user_dictFile.Close()
	scanner_u := bufio.NewScanner(user_dictFile)
	scanner_u.Split(bufio.ScanLines)
	for scanner_u.Scan() {
		slice_user = append(slice_user, scanner_u.Text())
	}

	pass_dictFile, _ := os.Open(pass_dict)
	defer pass_dictFile.Close()
	scanner_p := bufio.NewScanner(pass_dictFile)
	scanner_p.Split(bufio.ScanLines)
	for scanner_p.Scan() {
		slice_pass = append(slice_pass, scanner_p.Text())
	}

	return slice_iplist, slice_user, slice_pass
}

示例#24
文件: graph.go 项目: fiatjaf/nodes

func RenderGraph(dot string) bytes.Buffer {
	var out bytes.Buffer
	unflatten := exec.Command("unflatten", "-l 10")
	graphviz := exec.Command("dot", "-Tsvg")

	reader, writer := io.Pipe()

	unflatten.Stdin = strings.NewReader(dot)
	unflatten.Stdout = writer
	graphviz.Stdin = reader
	graphviz.Stdout = &out

	var stderr io.ReadCloser
	stderr, _ = unflatten.StderrPipe()
	go func(stderr io.ReadCloser) {
		in := bufio.NewScanner(stderr)
		for in.Scan() {
			log.Print(in.Text())
		}
	}(stderr)
	stderr, _ = graphviz.StderrPipe()
	go func(stderr io.ReadCloser) {
		in := bufio.NewScanner(stderr)
		for in.Scan() {
			log.Print(in.Text())
		}
	}(stderr)
	unflatten.Start()
	graphviz.Start()
	unflatten.Wait()
	writer.Close()
	graphviz.Wait()
	return out
}

示例#25
文件: Test_CLI.go 项目: enricodangelo/exercises-in-style

func main() {
	flag.Parse()
	flags := flag.Args()

	var text string
	var scanner *bufio.Scanner
	var err error

	if len(flags) > 0 {

		file, err := os.Open(flags[0])

		if err != nil {
			log.Fatal(err)
		}

		scanner = bufio.NewScanner(file)

	} else {
		scanner = bufio.NewScanner(os.Stdin)
	}

	for scanner.Scan() {
		text += scanner.Text()
	}

	err = scanner.Err()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(text)
}

示例#26
文件: anagram.go 项目: skhal/gopl_old

func main() {
	lineScanner := bufio.NewScanner(os.Stdin)
	for lineScanner.Scan() {
		line := lineScanner.Text()
		scanner := bufio.NewScanner(strings.NewReader(line))
		scanner.Split(bufio.ScanWords)
		var words []string
		for scanner.Scan() {
			word := scanner.Text()
			words = append(words, word)
			if len(words) == 2 {
				break
			}
		}
		if len(words) != 2 {
			continue
		}
		fmt.Printf("%q: ", words)
		if anagram(words[0], words[1]) {
			fmt.Println("anagrams")
		} else {
			fmt.Println("not anagrams")
		}
	}
}

示例#27
文件: main.go 项目: andywxl/ink

func Publish() {
	command := globalConfig.Build.Publish
	// Prepare exec command
	var shell, flag string
	if runtime.GOOS == "windows" {
		shell = "cmd"
		flag = "/C"
	} else {
		shell = "/bin/sh"
		flag = "-c"
	}
	cmd := exec.Command(shell, flag, command)
	cmd.Dir = filepath.Join(rootPath, "public")
	// Start print stdout and stderr of process
	stdout, _ := cmd.StdoutPipe()
	stderr, _ := cmd.StderrPipe()
	out := bufio.NewScanner(stdout)
	err := bufio.NewScanner(stderr)
	// Print stdout
	go func() {
		for out.Scan() {
			Log(out.Text())
		}
	}()
	// Print stdin
	go func() {
		for err.Scan() {
			Log(err.Text())
		}
	}()
	// Exec command
	cmd.Run()
}

示例#28
文件: library_android.go 项目: Raskal8/go-ethereum

func init() {
	// Redirect the standard output and error to logcat
	oldStdout, oldStderr := os.Stdout, os.Stderr

	outRead, outWrite, _ := os.Pipe()
	errRead, errWrite, _ := os.Pipe()

	os.Stdout = outWrite
	os.Stderr = errWrite

	go func() {
		scanner := bufio.NewScanner(outRead)
		for scanner.Scan() {
			line := scanner.Text()
			C.__android_log_write(C.ANDROID_LOG_INFO, C.CString("Stdout"), C.CString(line))
			oldStdout.WriteString(line + "\n")
		}
	}()

	go func() {
		scanner := bufio.NewScanner(errRead)
		for scanner.Scan() {
			line := scanner.Text()
			C.__android_log_write(C.ANDROID_LOG_INFO, C.CString("Stderr"), C.CString(line))
			oldStderr.WriteString(line + "\n")
		}
	}()
}

示例#29
文件: plugin.go 项目: nlamirault/docker-machine-scaleway

func (lbe *LocalBinaryExecutor) Start() (*bufio.Scanner, *bufio.Scanner, error) {
	log.Debugf("Launching plugin server for driver %s", lbe.DriverName)

	binaryPath, err := exec.LookPath(fmt.Sprintf("docker-machine-driver-%s", lbe.DriverName))
	if err != nil {
		return nil, nil, fmt.Errorf("Driver %q not found. Do you have the plugin binary accessible in your PATH?", lbe.DriverName)
	}

	log.Debugf("Found binary path at %s", binaryPath)

	cmd := exec.Command(binaryPath)

	lbe.pluginStdout, err = cmd.StdoutPipe()
	if err != nil {
		return nil, nil, fmt.Errorf("Error getting cmd stdout pipe: %s", err)
	}

	lbe.pluginStderr, err = cmd.StderrPipe()
	if err != nil {
		return nil, nil, fmt.Errorf("Error getting cmd stderr pipe: %s", err)
	}

	outScanner := bufio.NewScanner(lbe.pluginStdout)
	errScanner := bufio.NewScanner(lbe.pluginStderr)

	os.Setenv(PluginEnvKey, PluginEnvVal)

	if err := cmd.Start(); err != nil {
		return nil, nil, fmt.Errorf("Error starting plugin binary: %s", err)
	}

	return outScanner, errScanner, nil
}

示例#30
文件: scalable_test.go 项目: postfix/bloom-1

func init() {
	file, err := os.Open("/usr/share/dict/web2")
	if err != nil {
		fmt.Println("Cannot open /usr/share/dict/web2 - " + err.Error())
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		web2 = append(web2, scanner.Text())
	}

	if err = scanner.Err(); err != nil {
		fmt.Println("Error reading file - " + err.Error())
	}

	file2, err2 := os.Open("/usr/share/dict/web2a")
	if err2 != nil {
		fmt.Println("Cannot open /usr/share/dict/web2a - " + err2.Error())
	}
	defer file2.Close()

	scanner = bufio.NewScanner(file2)
	for scanner.Scan() {
		web2a = append(web2a, scanner.Text())
	}

	if err2 = scanner.Err(); err2 != nil {
		fmt.Println("Error reading file - " + err2.Error())
	}
}

展开阅读全文