英文:
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, "<string>")
, 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.Writer
。io.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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论