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


Golang io.ReadFile函数代码示例

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


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

示例1: prettyPaste

func prettyPaste(id string, limit int) (code []string, err os.Error) {
	source, err := io.ReadFile("pastes" + path.Clean("/"+id))
	if err != nil {
		return
	}

	multi := strings.Split(string(source), "\n---", 0)

	allCode := make([]string, len(multi))
	results := make(chan int)
	for i := 0; i < len(multi); i++ {
		go func(i int) {
			allCode[i], _ = prettySource(id, multi[i], limit)
			results <- i
		}(i)
	}

	for i := 0; i < len(multi); i++ {
		<-results
	}

	code = allCode

	return
}
开发者ID:necrogami,项目名称:go-play,代码行数:25,代码来源:pretty.go

示例2: readinfofile

func readinfofile(name string) ([]zonetime, bool) {
	buf, err := io.ReadFile(name)
	if err != nil {
		return nil, false
	}
	return parseinfo(buf)
}
开发者ID:8l,项目名称:go-learn,代码行数:7,代码来源:zoneinfo.go

示例3: ReadConfig

func ReadConfig() *Config {
	content, err := io.ReadFile(configFilename)
	if err != nil {
		log.Exitf("Can't open config file: %s\n", err)
	}
	return ParseConfig(string(content))
}
开发者ID:stefanha,项目名称:bouncin,代码行数:7,代码来源:config.go

示例4: Parse

func Parse(filename string) (*parser, os.Error) {
	var p parser
	input, err := io.ReadFile(filename)
	p.init(filename, input)
	p.Parse()
	return &p, err
}
开发者ID:jordanlewis,项目名称:gomps,代码行数:7,代码来源:parser.go

示例5: TestWriter

func TestWriter(t *testing.T) {
testLoop:
	for i, test := range writerTests {
		expected, err := io.ReadFile(test.file)
		if err != nil {
			t.Errorf("test %d: Unexpected error: %v", i, err)
			continue
		}

		buf := new(bytes.Buffer)
		tw := NewWriter(iotest.TruncateWriter(buf, 4<<10)) // only catch the first 4 KB
		for j, entry := range test.entries {
			if err := tw.WriteHeader(entry.header); err != nil {
				t.Errorf("test %d, entry %d: Failed writing header: %v", i, j, err)
				continue testLoop
			}
			if _, err := io.WriteString(tw, entry.contents); err != nil {
				t.Errorf("test %d, entry %d: Failed writing contents: %v", i, j, err)
				continue testLoop
			}
		}
		if err := tw.Close(); err != nil {
			t.Errorf("test %d: Failed closing archive: %v", err)
			continue testLoop
		}

		actual := buf.Bytes()
		if !bytes.Equal(expected, actual) {
			t.Errorf("test %d: Incorrect result: (-=expected, +=actual)\n%v",
				i, bytediff(expected, actual))
		}
	}
}
开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:33,代码来源:writer_test.go

示例6: main

func main() {
	flag.Parse()

	var filename string
	switch flag.NArg() {
	case 0:
		filename = "/dev/stdin"
	case 1:
		filename = flag.Arg(0)
	default:
		usage()
	}

	src, err := io.ReadFile(filename)
	if err != nil {
		scanner.PrintError(os.Stderr, err)
	}

	if path.Ext(filename) == ".html" {
		src = extractEBNF(src)
	}

	grammar, err := ebnf.Parse(filename, src)
	if err != nil {
		scanner.PrintError(os.Stderr, err)
	}

	if err = ebnf.Verify(grammar, *start); err != nil {
		scanner.PrintError(os.Stderr, err)
	}
}
开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:31,代码来源:ebnflint.go

示例7: ReadAll

// ReadAll lee el archivo .ini especificado en la estructura.
// Crea un mapa de mapas representando la estructura del archivo,
// separando secciones y elementos dentro de la seccion y lo almacena
// en la variable Seccion de la estructura InitFile.
func (iF *InitFile) ReadAll() (error os.Error) {
	micontenido := make(contenido)
	miseccion := "default"
	if mifichero, error := io.ReadFile(iF.nombre); error != nil {
		return error
	} else {
		cadena := string(mifichero)
		lineas := strings.Split(cadena, "\n", 0)
		for _, linea := range lineas {
			expresion := strings.TrimSpace(linea)
			if len(expresion) == 0 {
				continue
			}
			if expresion[0] == comentario {
				continue
			}
			if expresion[0] == corchete && expresion[len(expresion)-1] == cierracorchete {
				iF.Seccion[miseccion] = micontenido
				miseccion = string(expresion[1 : len(expresion)-1])
				micontenido = make(contenido)
				continue
			}
			valores := strings.Split(expresion, "=", 2)
			if len(valores) != 2 {
				continue
			}
			micontenido[valores[0]] = valores[1]
		}
	}
	if _, ok := iF.Seccion[miseccion]; !ok {
		iF.Seccion[miseccion] = micontenido
	}
	return nil
}
开发者ID:nether,项目名称:LibreriaGo,代码行数:38,代码来源:inix.go

示例8: readSource

// If src != nil, readSource converts src to a []byte if possible;
// otherwise it returns an error. If src == nil, readSource returns
// the result of reading the file specified by filename.
//
func readSource(filename string, src interface{}) ([]byte, os.Error) {
	if src != nil {
		switch s := src.(type) {
		case string:
			return strings.Bytes(s), nil
		case []byte:
			return s, nil
		case *bytes.Buffer:
			// is io.Reader, but src is already available in []byte form
			if s != nil {
				return s.Bytes(), nil
			}
		case io.Reader:
			var buf bytes.Buffer
			_, err := io.Copy(&buf, s)
			if err != nil {
				return nil, err
			}
			return buf.Bytes(), nil
		default:
			return nil, os.ErrorString("invalid source")
		}
	}

	return io.ReadFile(filename)
}
开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:30,代码来源:interface.go

示例9: banana

func banana(c *http.Conn, req *http.Request) {
	contents, err := io.ReadFile("index.html")
	if err != nil {
		panic("ListenAndServe: ", err.String())
	}
	io.WriteString(c, string(contents))
}
开发者ID:machinaut,项目名称:book-checker,代码行数:7,代码来源:server.go

示例10: TestFiles

func TestFiles(t *testing.T) {
	for _, filename := range files {
		src, err := io.ReadFile(filename)
		if err != nil {
			t.Fatal(err)
		}
		check(t, filename, src)
	}
}
开发者ID:machinaut,项目名称:go-play,代码行数:9,代码来源:peg_test.go

示例11: loadTemplate

func loadTemplate(path string) (t *template.Template, err os.Error) {
	if buf, readErr := io.ReadFile("templates/" + path); readErr == nil {
		t = template.MustParse(string(buf), template.FormatterMap{
			"html": template.HTMLFormatter
		});
	} else {
		err = readErr
	}
	return;
}
开发者ID:tung,项目名称:goblog,代码行数:10,代码来源:views.go

示例12: main

func main() {
	maths, err := io.ReadFile("math.peg")
	if err != nil {
		log.Exit("Error: ", err)
	}

	fmt.Println(maths)
	rune, size := utf8.DecodeRuneInString(string(maths))
	fmt.Println(rune, size)
	return
}
开发者ID:machinaut,项目名称:go-play,代码行数:11,代码来源:foo.go

示例13: readTemplate

func readTemplate(name string) *template.Template {
	path := pathutil.Join(*tmplroot, name)
	data, err := io.ReadFile(path)
	if err != nil {
		log.Exitf("ReadFile %s: %v", path, err)
	}
	t, err := template.Parse(string(data), fmap)
	if err != nil {
		log.Exitf("%s: %v", name, err)
	}
	return t
}
开发者ID:8l,项目名称:go-learn,代码行数:12,代码来源:godoc.go

示例14: serveTextFile

func serveTextFile(c *http.Conn, r *http.Request, path string) {
	src, err := io.ReadFile(path)
	if err != nil {
		log.Stderrf("serveTextFile: %s", err)
	}

	var buf bytes.Buffer
	fmt.Fprintln(&buf, "<pre>")
	template.HTMLEscape(&buf, src)
	fmt.Fprintln(&buf, "</pre>")

	servePage(c, "Text file "+path, "", buf.Bytes())
}
开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:13,代码来源:godoc.go

示例15: Get

// TODO: malicious ids
func (ps *PersistService) Get(id string) (*Model, bool) {
	log.Stderrf("PS:GET:%s", id);
	mpath := path.Join(ps.Path, id);
	log.Stderrf("PS:GET:path:%s", mpath);
	bs, e := io.ReadFile(mpath);
	// TODO: check error more specifically (not found, etc.)
	if e != nil {
		log.Stderrf("PS:GET:e:%s", e);
		return nil, false;
	}
	else {
		return parse_model(id, bs), true;
	}
	panic("unreachable");
}
开发者ID:montsamu,项目名称:go-twitter-oauth,代码行数:16,代码来源:persist.go


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