golang io/ioutil包简单使用

package main

import (
	"fmt"
	"io/ioutil"
	"strings"
)

func main() {
	r1 := strings.NewReader("aaa")
	//返回ReadCloser对象提供close函数
	rc1 := ioutil.NopCloser(r1)
	defer rc1.Close()

	//ReadAll读取所有数据
	p, _ := ioutil.ReadAll(strings.NewReader("12345"))
	fmt.Println(string(p))

	//ReadDir返回目录下所有文件切片
	fileInfo, _ := ioutil.ReadDir("./")
	for _, data := range fileInfo {
		fmt.Println(data.Name())
	}

	//读取整个文件数据
	data, _ := ioutil.ReadFile("./1.rtf")
	fmt.Println(string(data))

	//创建文件,存在清空文件
	ioutil.WriteFile("./1.txt", []byte("111"), 0655)

	//创建指定前缀的临时文件夹,返回文件夹名称
	dir, _ := ioutil.TempDir("./", "test")
	fmt.Println(dir)

	//创建test为前缀的临时文件,返回os.File指针
	f, _ := ioutil.TempFile("./", "test")
	f.Write([]byte("222"))
	f.Close()
}

  

原文地址:https://www.cnblogs.com/lemonzwt/p/10213234.html

时间: 2024-08-09 06:12:33

golang io/ioutil包简单使用的相关文章

io/ioutil包

// Discard 是一个 io.Writer 接口,调用它的 Write 方法将不做任何事情 // 并且始终成功返回. var Discard io.Writer = devNull(0) // ReadAll 读取 r 中的所有数据,返回读取的数据和遇到的错误. // 如果读取成功,则 err 返回 nil,而不是 EOF,因为 ReadAll 定义为读取 // 所有数据,所以不会把 EOF 当做错误处理. func ReadAll(r io.Reader) ([]byte, error)

io/ioutil

io/ioutil包实现了一些 i/o 操作的函数 package main import ( "fmt" "io/ioutil" "os" "strings" ) func main() { dirname, err := ioutil.ReadDir("../") //获取dirname指定的目录的目录信息的有序列表. fmt.Println(err) for k, v := range dirname

简析 Golang IO 包

简析 Golang IO 包 io 包提供了 I/O 原语(primitives)的基本接口.io 包中定义了四个最基本接口 Reader.Writer.Closer.Seeker 用于表示二进制流的读.写.关闭和寻址操作.这些原语和接口是对底层操作的封装,因此如没有特殊说明,这些原语和接口都不能被视为线程安全的. Reader Reader 接口封装了基本的 Read 方法.Read 读取长度为 len(p) 字节的数据,并写入到 p.返回结果包含读取数据字节数(0 <= n <= len(

Golang 文件读写之 os, bufio, io/ioutil 初体验

package main   import (      //"bufio"      "fmt"      //"io"      "io/ioutil"      "os"      "time"  )   func main() {       file1, err := os.OpenFile("hehehe.txt", os.O_RDWR|os.O_APPE

ioutil包二

ioutil包二 (原创随笔,转载请注明出处 http://www.cnblogs.com/majianguo/p/8016426.html) ioutil包实现了一些I/O实用功能,导出了7个函数和1个变量: func NopCloser(r io.Reader) io.ReadCloser func ReadAll(r io.Reader) ([]byte, error) func ReadDir(dirname string) ([]os.FileInfo, error) func Rea

Golang官方log包详解

Golang官方log包详解 以下全是代码, 详解在注释中, 请从头到尾看 // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Package log implements a simple logging package. I

golang的sync包例子

package main import ( "fmt" "sync" ) var wg sync.WaitGroup func asyncTestFunc() { for i := 0; i < 100; i++ { fmt.Println(i) } wg.Done() } func main() { wg.Add(1) go asyncTestFunc() wg.Wait() } golang的sync包例子

使用golang的pprof包对程序进行性能分析

程序经常出现OOM错误,然后关键字"go pprof"搜到文章<Go程序性能分析pprof>,该文章第二步说运行程序后会生成profile文件,但是编译运行后发现生成的profile文件大小一直为0,然后关键字"go pprof profile is empty"搜到文章<Golang pprof heap profile is empty>,该文章说在运行程序前添加环境变量GODEBUG="memprofilerate=1&quo

Golang Gin 项目包依赖管理 godep 使用

Golang Gin 项目包依赖管理 godep 使用 标签(空格分隔): Go 在按照github.com/tools/godep文档go get完包以后,调整项目结构为$GOPATH/src/$PROJECT_NAME/,同时使项目编译没有问题.执行godep save命令,出现了一系列包缺失的问题: github.com/campoy/embedmd github.com/client9/misspell/cmd/misspell github.com/dustin/go-broadcas