我需要知道为什么这段代码不能编译,go编译器一直告诉我使用invalid-utf编码,但我不明白这究竟发生了什么。我收到的错误。
$ go build Example#2.go
command-line-arguments
.\Example#2.go:32:15: invalid UTF-8 encoding
对此的任何帮助都将非常感激。我知道它是一个简单的程序,但它是为了向我的编程语言类演示语言,这只是一个简单的例子,展示了方法名称必须如何链接到结构以及接口如何工作。就像我说的那样,它适用于go playground
但不适用于实际的编译器。
package main
import (
"fmt"
)
type Person struct {
Name string
Address Address
}
type Address struct {
Number string
Street string
City string
State string
Zip string
}
type Citizen struct {
Country string
Person
}
type speak interface {
Talk()
}
func (p *Person) Talk() {
fmt.Println("Hi, my name is", p.Name)
}
func (p *Person) Location() {
fmt.Printf("I’m at %v %v %v %v %v\n", p.Address.Number,
p.Address.Street, p.Address.City, p.Address.State, p.Address.Zip)
}
func (c *Citizen) Nationality() {
fmt.Println(c.Name, "is a citizen of", c.Country)
}
func (c *Citizen) Talk() {
fmt.Println("this is the citizen talk method")
}
func Converse(s speak) {
s.Talk()
}
func main() {
p := Person{
Name: "Jim",
Address: Address{
Number: "1",
Street: "College Avenue",
City: "exampleland",
State: "StateLand",
Zip: "8675309",
},
}
c := Citizen{}
c.Name = "John"
c.Country = "Somewhereland"
c.Person.Address.Number = "1337"
c.Person.Address.Street = "Haxor Lane"
c.Person.Address.City = "Lazy"
c.Person.Address.State = "Foo"
c.Person.Address.Zip = "192.168.1.1"
p.Talk()
p.Location()
///////////////
fmt.Println("")
///////////////
c.Person.Talk()
c.Talk()
///////////////
fmt.Println("")
///////////////
c.Nationality()
c.Location()
///////////////
fmt.Println("")
Converse(&p)
Converse(&c)
}
答案 0 :(得分:0)
原来这是由“I'm”//>>中的撇号引起的。 fmt.Printf(“我在%v%v%v%v%v \ n”,p.Address.Number,p.Address.Street,p.Address.City,p.Address.State,p.Address的.zip)