golang如何读取文件名中包含空格的文件
环境:
win8.1 x64, go1.3 x64
程序调用方式:
问题描述:
只要是当前文件中含有空格程序就报错,错误如下:
The system cannot find the file specified. goroutine 16 [running]: runtime.panic(0x4d6740, 0xc082005d70) c:/go/src/pkg/runtime/panic.c:279 +0x11f main.is_file(0xc082000170, 0x7) D:/www/scripts/go/test/test.go:24 +0x8b main.main() D:/www/scripts/go/test/test.go:15 +0x9a goroutine 17 [runnable]: runtime.MHeap_Scavenger() c:/go/src/pkg/runtime/mheap.c:507 runtime.goexit() c:/go/src/pkg/runtime/proc.c:1445 goroutine 18 [runnable]: bgsweep() c:/go/src/pkg/runtime/mgc0.c:1976 runtime.goexit() c:/go/src/pkg/runtime/proc.c:1445 goroutine 19 [runnable]: runfinq() c:/go/src/pkg/runtime/mgc0.c:2606 runtime.goexit() c:/go/src/pkg/runtime/proc.c:1445
程序内容:
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
)
func main() {
file_name := flag.String("f", "", "文件名")
flag.Parse()
if *file_name != "" {
is_file(*file_name)
} else {
fmt.Printf("test\n")
}
}
func is_file(file_name string) {
file, err := os.Open(file_name)
if err != nil {
panic(err)
}
defer file.Close()
input, _ := ioutil.ReadAll(file)
fmt.Println(input)
}
谢谢...