本文整理汇总了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
}
示例2: readinfofile
func readinfofile(name string) ([]zonetime, bool) {
buf, err := io.ReadFile(name)
if err != nil {
return nil, false
}
return parseinfo(buf)
}
示例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))
}
示例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
}
示例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))
}
}
}
示例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)
}
}
示例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
}
示例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)
}
示例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))
}
示例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)
}
}
示例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;
}
示例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
}
示例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
}
示例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())
}
示例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");
}