在Go语言中,当我使用fmt.Printf时,文件的类型是什么?

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

In Go, of what type is a file when I'm using fmt.Printf?

问题

我正在使用fmt.Fprintf(file, "<string>"),但是在一个函数中。我在主函数中使用xmlFile :=创建了新的文件,然后将该变量传递给写入文件的函数调用,但是我得到了一个关于文件参数没有类型的错误。我应该给它什么类型?

它不是一个字符串,它是一个文件。

文件的类型是什么?

函数的定义如下:

func processTopic(id string, properties map[string][]string, file) {
英文:

I'm using fmt.Fprintf(file, &quot;&lt;string&gt;&quot;), but in a function. I create the new file using xmlFile := in the main function and then pass that variable to the call of a function that writes to the file, but I am getting an error for the file parametre that it doesn't have a type. What type should I give it?

It is not a string, it is a file.

Of what type is a file?

Here is what the definition of the function looks like:

func processTopic(id string, properties map[string][]string, file){

答案1

得分: 3

文档中:

func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)

Fprintf接受一个io.Writerio.Writer是一个接口,这意味着你可以传递任何实现了该接口的类型。这包括*os.File(系统文件)和*bytes.Buffer(内存)。

英文:

From the documentation:

func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)

Fprintf accepts an io.Writer. io.Writer is an interface which means that you an pass any type that implements it. This includes *os.File (system file) and *bytes.Buffer (memory).

huangapple
  • 本文由 发表于 2014年4月15日 04:55:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/23070221.html
匿名

发表评论

匿名网友

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

确定