【问题标题】:Why doesnt this compile with go 1.10 compiler but will run on goplayground为什么这不能用 go 1.10 编译器编译但会在 go playground 上运行
【发布时间】:2018-04-16 03:02:43
【问题描述】:

我需要知道为什么这段代码无法编译,go 编译器一直告诉我我使用了无效的 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)
}

【问题讨论】:

  • go 1.10.1 这里,它编译并运行良好。
  • 您是否在十六进制编辑器中查看过该文件以查看其中是否有一些时髦的字节?
  • 不,我没有,但是当我在我的 linux VM 上运行工作时,它似乎没有这个问题,它的 centos7 和我的个人机器上编译问题是 windows 10 home。
  • 谢谢 mu,我从来没有想过使用十六进制编辑器,因为我以前从未使用过它们,但是我下载了一个,并且 bam,单个字符在 UTF-8 中没有正确注册,它是撇号“'”。
  • ' 是一个完全有效的字符。在 go 字符串字面量中使用它是安全的。

标签: go utf-8 interface


【解决方案1】:

原来这是由“I'm”中的撇号引起的 //>> 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 2011-12-08
    相关资源
    最近更新 更多