在Go语言中读取XML文件。

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

Read an XML file in Go

问题

我在Go语言中写了一段小代码,我认为它足够好来读取一个XML文件。

有人知道发生了什么吗?

XML文件:

<Lang>
	<Name> Go </Name>
	<Year> 2009 </Year>
	<URL> http://golang.org/ </URL>

</Lang>

Go代码:

package main

import (
	"io"
	"log"
	"os"
)

func main() {
	input, err := os.Open("C:\GoWork\toy\lang.xml")
	if err != nil {
		log.Fatal(err)
	}
	io.Copy(os.Stdout, input)
}

错误信息:

.\xmltoStruct.go:11: 未知的转义序列:G
.\xmltoStruct.go:11: 未知的转义序列:l
英文:

I did a small code in Go, that I thought it was good enough to read an XML file.

Can someone know what is happing?

XML file:

<Lang>
	<Name> Go </Name>
	<Year> 2009 </Year>
	<URL> http://golang.org/ </URL>

</Lang>

Go code:

package main

import (
	"io"
	"log"
	"os"
)

func main() {
	input, err := os.Open("C:\GoWork\toy\lang.xml")
	if err != nil {
		log.Fatal(err)
	}
	io.Copy(os.Stdout, input)
}

Error message:

.\xmltoStruct.go:11: unknown escape sequence: G
.\xmltoStruct.go:11: unknown escape sequence: l

答案1

得分: 2

双引号之间的字符串字面值是一个解释的字符串字面值,其中反斜杠是一个特殊字符,表示转义序列。请参阅规范:字符串字面值

引号之间的文本形成字面值的值,其中反斜杠转义被解释为rune字面值中的转义(除了'是非法的,"是合法的),具有相同的限制。

要么在反斜杠前加上双引号:

input, err := os.Open("C:\\GoWork\\toy\\lang.xml")

或者更简单地使用一个原始字符串字面值(反引号),其中反斜杠没有特殊含义:

input, err := os.Open(`C:\GoWork\toy\lang.xml`)

此外,你应该关闭文件,最好使用延迟语句:

input, err := os.Open(`C:\GoWork\toy\lang.xml`)
if err != nil {
    log.Fatal(err)
}
defer input.Close()
io.Copy(os.Stdout, input)
英文:

A string literal between double quotes is an interpreted string literal where backslash is a special character denoting escape sequences. See Spec: String literals:

> The text between the quotes forms the value of the literal, with backslash escapes interpreted as they are in rune literals (except that ' is illegal and " is legal), with the same restrictions.

Either double quote your backslashes:

input, err := os.Open("C:\\GoWork\\toy\\lang.xml")

Or easier: use a raw string literal (backticks) where backslash has no special meaning:

input, err := os.Open(`C:\GoWork\toy\lang.xml`)

Also you should close your file, preferably as a deferred statement:

input, err := os.Open(`C:\GoWork\toy\lang.xml`)
if err != nil {
    log.Fatal(err)
}
defer input.Close()
io.Copy(os.Stdout, input)

答案2

得分: 0

当前这是两种文字形式的混合:

input, err := os.Open("C:\GoWork\toy\lang.xml")

基本上有两种形式的字符串文字:
a. 原始字符串文字
b. 解释字符串文字

可以使用原始字符串文字,即反引号 `foo`,在引号之间的文字值没有特殊含义。

或者使用解释字符串文字,即双引号 "bar",但要注意,这些值会像rune literals一样被解释。

英文:

Currently this is mix of two literal forms

input, err := os.Open("C:\GoWork\toy\lang.xml")

There are basically two forms of string literals:
a. raw string literals
b. interpreted string literals

either use raw string literal with back quotes, as in `foo` and the value of literal has no special meaning between the quotes

or interpreted string literals with double quotes, as in "bar" but take precaution because the value are interpreted as in rune literals

huangapple
  • 本文由 发表于 2016年11月7日 09:52:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/40456725.html
匿名

发表评论

匿名网友

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

确定