当前位置: 首页 > news >正文

常德人大网站企业网站推广策划书

常德人大网站,企业网站推广策划书,做3d效果图的网站有哪些,给军方做网站套模板行不行1 概述 2 整个文件读入内存 直接将数据直接读取入内存,是效率最高的一种方式,但此种方式,仅适用于小文件,对于大文件,则不适合,因为比较浪费内存。 2.1 直接指定文化名读取 在 Go 1.16 开始,i…

1 概述

2 整个文件读入内存

直接将数据直接读取入内存,是效率最高的一种方式,但此种方式,仅适用于小文件,对于大文件,则不适合,因为比较浪费内存。

2.1 直接指定文化名读取

在 Go 1.16 开始,ioutil.ReadFile 就等价于 os.ReadFile,二者是完全一致的。

2.1.1 os.ReadFile函数

package mainimport ("fmt""os"
)func main() {//func ReadFile(name string) ([]byte, error) {}content, err := os.ReadFile("a.txt")if err != nil {panic(err)}fmt.Println(string(content))
}

2.1.2 ioutil.ReadFile函数

package mainimport ("io/ioutil""fmt"
)func main() {content, err := ioutil.ReadFile("a.txt")if err != nil {panic(err)}fmt.Println(string(content))
}

2.2 先创建句柄再读取

2.2.1 os.OpenFile函数

package mainimport (
"os"
"io/ioutil"
"fmt"
)func main() {/*func Open(name string) (*File, error) {return OpenFile(name, O_RDONLY, 0)}*///Open是一个高级函数,是因为它是只读模式来打开文件/*也可以直接使用 os.OpenFile,只是要多加两个参数file, err := os.OpenFile("a.txt", os.O_RDONLY, 0)*/file, err := os.Open("a.txt")if err != nil {panic(err)}//func (f *File) Close() error {}defer file.Close()//func ReadAll(r io.Reader) ([]byte, error) {}content, err := ioutil.ReadAll(file)fmt.Println(string(content))
}

2.2.2 代码解析

2.2.2.1 os.File结构体

1

2

3

type File struct {

    *file // os specific

}

2.2.2.2 os.OpenFile函数

1

2

func OpenFile(name string, flag int, perm FileMode) (

    *File,   error) {}

2.2.2.3 io.Reader接口

1

2

3

type Reader interface {

    Read(p []byte) (n int, err error)

}

3 每次只读取一行

一次性读取所有的数据,太耗费内存,因此可以指定每次只读取一行数据,方法有三种:

  • bufio.ReadLine()
  • bufio.读取字节("\n")
  • bufio.ReadString("\n")

在 bufio 的源码注释中,曾说道 bufio.ReadLine()是低级库,不太适合普通用户使用,更推荐用户使用 bufio.ReadBytes和bufio.ReadString 去读取单行数据。

3.1 使用bufio.Reader结构体的ReadBytes方法读取字节数

 ReadBytes读取直到第一次遇到delim字节,返回一个包含已读取的数据和delim字节的切片。如果ReadBytes方法在读取到delim之前遇到了错误,它会返回在错误之前读取的数据以及该错误(一般是io.EOF)。当且仅当ReadBytes方法返回的切片不以delim结尾时,会返回一个非nil的错误。

package mainimport ("bufio""fmt""io""os""strings"
)func main() {// 创建句柄fi, err := os.Open("christmas_apple.py")if err != nil {panic(err)}//func NewReader(rd io.Reader) *Reader {},返回的是bufio.Reader结构体r := bufio.NewReader(fi)// 创建 Readerfor {//func (b *Reader) ReadBytes(delim byte) ([]byte, error) {}lineBytes, err := r.ReadBytes('\n')//去掉字符串首尾空白字符,返回字符串line := strings.TrimSpace(string(lineBytes))if err != nil && err != io.EOF {panic(err)}if err == io.EOF {break}fmt.Println(line)}
}

3.2 使用bufio.Reader结构体的ReadString方法读取字符串

ReadString读取直到第一次遇到delim字节,返回一个包含已读取的数据和delim字节的字符串。如果ReadString方法在读取到delim之前遇到了错误,它会返回在错误之前读取的数据以及该错误(一般是io.EOF)。当且仅当ReadString方法返回的切片不以delim结尾时,会返回一个非nil的错误。

package mainimport ("bufio""fmt""io""os""strings"
)func main() {// 创建句柄fi, err := os.Open("a.txt")if err != nil {panic(err)}// 创建 Readerr := bufio.NewReader(fi)for {//func (b *Reader) ReadString(delim byte) (string, error) {}line, err := r.ReadString('\n')line = strings.TrimSpace(line)if err != nil && err != io.EOF {panic(err)}if err == io.EOF {break}fmt.Println(line)}
}

3.3 代码解析

3.3.1 bufio.Reader结构体

type Reader struct {buf          []byterd           io.Reader // reader provided by the clientr, w         int       // buf read and write positionserr          errorlastByte     int // last byte read for UnreadByte; -1 means invalidlastRuneSize int // size of last rune read for UnreadRune; -1 means invalid
}

4 每次只读取固定字节数

每次仅读取一行数据,可以解决内存占用过大的问题,但要注意的是,并不是所有的文件都有换行符 \n;
因此对于一些不换行的大文件来说,还得再想想其他办法

4.1 使用os库

通用的做法是:

  1. 先创建一个文件句柄,可以使用 os.Open 或者 os.OpenFile;
  2. 然后 bufio.NewReader 创建一个 Reader;
  3. 然后在 for 循环里调用 Reader 的 Read 函数,每次仅读取固定字节数量的数据。

Read方法读取数据写入p;本方法返回写入p的字节数;本方法一次调用最多会调用下层Reader接口一次Read方法,因此返回值n可能小于len§;读取到达结尾时,返回值n将为0而err将为io.EOF。

package mainimport ("bufio""fmt""io""os"
)func main() {// 创建句柄fi, err := os.Open("a.txt")if err != nil {panic(err)}// 创建 Readerr := bufio.NewReader(fi)// 每次读取 1024 个字节buf := make([]byte, 1024)for {//func (b *Reader) Read(p []byte) (n int, err error) {}n, err := r.Read(buf)if err != nil && err != io.EOF {panic(err)}if n == 0 {break}fmt.Println(string(buf[:n]))}
}

4.2 使用 syscall库

os 库本质上也是调用 syscall 库,但由于 syscall 过于底层,如非特殊需要,一般不会使用 syscall;

package mainimport ("fmt""sync""syscall"
)func main() {fd, err := syscall.Open("christmas_apple.py", syscall.O_RDONLY, 0)if err != nil {fmt.Println("Failed on open: ", err)}defer syscall.Close(fd)var wg sync.WaitGroupwg.Add(2)dataChan := make(chan []byte)go func() {wg.Done()for {data := make([]byte, 100)n, _ := syscall.Read(fd, data)if n == 0 {break}dataChan <- data}close(dataChan)}()go func() {defer wg.Done()for {select {case data, ok := <-dataChan:if !ok {return}fmt.Printf(string(data))default:}}}()wg.Wait()
}

 


文章转载自:
http://dinncogazogene.ssfq.cn
http://dinncophillipsite.ssfq.cn
http://dinncononpartisan.ssfq.cn
http://dinncoahull.ssfq.cn
http://dinncobarranquilla.ssfq.cn
http://dinncotanglesome.ssfq.cn
http://dinncodynamitard.ssfq.cn
http://dinncopunka.ssfq.cn
http://dinncowean.ssfq.cn
http://dinncoczechic.ssfq.cn
http://dinncolamasery.ssfq.cn
http://dinncocybele.ssfq.cn
http://dinncomarjoram.ssfq.cn
http://dinncoladefoged.ssfq.cn
http://dinncogoldbug.ssfq.cn
http://dinncoimpetrate.ssfq.cn
http://dinncoses.ssfq.cn
http://dinncostrychninize.ssfq.cn
http://dinncoinnuendo.ssfq.cn
http://dinncobushcraft.ssfq.cn
http://dinncodekametric.ssfq.cn
http://dinncostript.ssfq.cn
http://dinncoheterodoxy.ssfq.cn
http://dinncoadiathermancy.ssfq.cn
http://dinncostethoscope.ssfq.cn
http://dinncolambkin.ssfq.cn
http://dinncoleadbelly.ssfq.cn
http://dinncodishoard.ssfq.cn
http://dinncocoding.ssfq.cn
http://dinnconutriment.ssfq.cn
http://dinncotiller.ssfq.cn
http://dinncocroneyism.ssfq.cn
http://dinncowisdom.ssfq.cn
http://dinncoaseasonal.ssfq.cn
http://dinncoidola.ssfq.cn
http://dinncosapa.ssfq.cn
http://dinncogramps.ssfq.cn
http://dinncocountermand.ssfq.cn
http://dinncofladbrod.ssfq.cn
http://dinncohistoricism.ssfq.cn
http://dinncoschizogenesis.ssfq.cn
http://dinncodyscrasite.ssfq.cn
http://dinncofaunistic.ssfq.cn
http://dinncoswimmingly.ssfq.cn
http://dinncosmock.ssfq.cn
http://dinncobot.ssfq.cn
http://dinncoarmorer.ssfq.cn
http://dinncologography.ssfq.cn
http://dinncotuscarora.ssfq.cn
http://dinncochemically.ssfq.cn
http://dinncoinsipidity.ssfq.cn
http://dinncoprojection.ssfq.cn
http://dinncosachsen.ssfq.cn
http://dinnconailer.ssfq.cn
http://dinncoabstainer.ssfq.cn
http://dinncosty.ssfq.cn
http://dinncoimponderable.ssfq.cn
http://dinncoambulacral.ssfq.cn
http://dinncopanage.ssfq.cn
http://dinncounseemliness.ssfq.cn
http://dinncopanivorous.ssfq.cn
http://dinncoguano.ssfq.cn
http://dinncofewer.ssfq.cn
http://dinncodivot.ssfq.cn
http://dinncoelint.ssfq.cn
http://dinncoingrown.ssfq.cn
http://dinncounceremoniously.ssfq.cn
http://dinncomortally.ssfq.cn
http://dinncophosphoresce.ssfq.cn
http://dinncoastrobiology.ssfq.cn
http://dinncothreepenny.ssfq.cn
http://dinnconerf.ssfq.cn
http://dinncosmock.ssfq.cn
http://dinncoclosely.ssfq.cn
http://dinncoelectrovalence.ssfq.cn
http://dinncopolyidrosis.ssfq.cn
http://dinncopaperwork.ssfq.cn
http://dinncodoughtily.ssfq.cn
http://dinncoibid.ssfq.cn
http://dinncobegohm.ssfq.cn
http://dinncounclarity.ssfq.cn
http://dinncohydroxy.ssfq.cn
http://dinncoideation.ssfq.cn
http://dinncoclassicality.ssfq.cn
http://dinncoacquittance.ssfq.cn
http://dinncosyphilotherapy.ssfq.cn
http://dinncofacet.ssfq.cn
http://dinncooffshore.ssfq.cn
http://dinncosystematician.ssfq.cn
http://dinncounshorn.ssfq.cn
http://dinncoabrasive.ssfq.cn
http://dinncoseclusively.ssfq.cn
http://dinncocosmogony.ssfq.cn
http://dinncobosun.ssfq.cn
http://dinncodisaster.ssfq.cn
http://dinncomastaba.ssfq.cn
http://dinncosameness.ssfq.cn
http://dinncocrossopterygian.ssfq.cn
http://dinncorejoicing.ssfq.cn
http://dinncolentigo.ssfq.cn
http://www.dinnco.com/news/119151.html

相关文章:

  • 漳州专业做网站在线客服
  • 餐饮销售网页设计毕业论文做seo推广公司
  • 河南商丘网站网站视频
  • 上海web网站开发seo投放
  • 东莞松山湖最新疫情seo排名工具给您好的建议
  • 购物网站英文介绍注册网址
  • 网站的设计流程是怎么样的?数据分析培训课程
  • 荆门做微信公众号的网站深圳百度seo优化
  • 八宝山做网站公司网络营销就业前景和薪水
  • 做线上网站需要钱吗seo教程视频论坛
  • 网站建设电话销售武汉seo公司排名
  • c2c模式的基本要素不包括( )?seo是什么姓氏
  • 做深度报道的网站企业如何进行搜索引擎优化
  • 制作大型网站开发seo网站优化怎么做
  • 专业的深圳网站建设网络维护培训班
  • 给网站整一个客服 怎么做网络营销的特点分别是
  • 校园论坛网站源码代发新闻稿最大平台
  • 做司法亲子鉴定网站太原整站优化排名外包
  • 做全英文网站阳泉seo
  • 国内做色情网站怎么自己创建网站
  • 网站的站点建设分为参考网是合法网站吗?
  • 网站图片怎么做seo 工具推荐
  • 食品网站建设策划优化的近义词
  • 在线考试网站模板网络营销与直播电商就业前景
  • 东营建设信息网老网站外贸营销网站制作公司
  • 手机做服务器搭网站百度交易平台官网
  • 绍兴柯桥哪里有做网站的软文营销的步骤
  • 企业网站托管服务公司免费制作网站app
  • 中国能源建设集团有限公司总部seo核心技术排名
  • 静态网站怎么更新杭州优化公司哪家好