PHP file_get_contents in Go lang

huangapple go评论68阅读模式
英文:

PHP file_get_contents in Go lang

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我是一个新手,对Golang不太熟悉。实际上,我是一个PHP开发者。

我需要在Golang中使用file_get_contents函数。

你能提供这个函数的代码或者建议一个适合我需求的Golang库吗?

注意:请记住,file-get-contents不仅仅是"读取文件"。

英文:

I'm new to Golang. Actually I'm a PHP developer.

I need file_get_contents function in Golang.

Can you please provide code for this or suggest Golang library for my requirement.

Note: Keep in mind file-get-contents does more than just "read a file".

答案1

得分: 7

我不认为有一个与file-get-contents.php一样多功能的独特的golang函数。

> 你经常希望对文件的读取方式和读取部分进行更多的控制。
对于这些任务,首先打开文件以获取os.File值。

f, err := os.Open("/tmp/dat")
check(err)

# 你也可以定位到文件中的已知位置并从那里读取。

o2, err := f.Seek(6, 0)
check(err)
b2 := make([]byte, 2)
n2, err := f.Read(b2)
check(err)
fmt.Printf("%d bytes @ %d: %s\n", n2, o2, string(b2))
英文:

I don't think there is one unique golang function as versatile as file-get-contents.php.

> You’ll often want more control over how and what parts of a file are read.
For these tasks, start by Opening a file to obtain an os.File value.

f, err := os.Open("/tmp/dat")
check(err)

# You can also Seek to a known location in the file and Read from there.

o2, err := f.Seek(6, 0)
check(err)
b2 := make([]byte, 2)
n2, err := f.Read(b2)
check(err)
fmt.Printf("%d bytes @ %d: %s\n", n2, o2, string(b2))

huangapple
  • 本文由 发表于 2014年8月2日 13:16:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/25091930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定