需要将使用”exp/utf8string”的代码翻译为后续标准库代码。

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

Need to translate code using "exp/utf8string" into later standard library code

问题

我正在尝试运行《The Go Programming Language Phrasebook》中的一个示例,该书是在2012年编写的,基于Go 1.0。该示例使用了exp/utf8string包,但现在已经变成了unicode/utf8包。我目前使用的是Go 1.2.1,下面列出的代码无法编译,因为exp/utf8string包已经废弃了:

package main
import "strings"
import "unicode"
import "exp/utf8string"
import "fmt"

func main() {
    str := "\tthe important rôles of utf8 text\n"
    str = strings.TrimFunc(str, unicode.IsSpace)

    // The wrong way
    fmt.Printf("%s\n", str[0:len(str)/2])
    // The right way
    u8 := utf8string.NewString(str)
    FirstHalf := u8.Slice(0, u8.RuneCount()/2)
    fmt.Printf("%s\n", FirstHalf)
}

作为GoLang的新手,我不确定旧的实验性包是如何集成到标准库中的。我进行了一些研究,发现utf8string.NewString(str)现在是expvar.NewString(str),所以我将导入语句修改为:

import "expvar"
import "unicode"

并相应地修改了代码以调用expvar.NewString(str),但仍然出现了两个错误:

u8.Slice未定义(*expvar.String类型没有Slice字段或方法)
u8.RuneCount未定义(*expvar.String类型没有RuneCount字段或方法)

我尝试了几种不同的方法,但似乎无法使其正常工作。

请问如何修改这个示例代码以适用于GoLang 1.2.1?

英文:

I am trying to ran an example from The Go Programming Language Phrasebook - the book was written in 2012, based on Go 1.0 . The example uses the exp/utf8string package, which has now become unicode/utf8. I am currently using Go 1.2.1 and code listed below will not compile as is, since the exp/utf8string package is now defunct:

package main
import "strings"
import "unicode"
import "exp/utf8string"
import "fmt"

func main()
{
    str := "\tthe important rôles of utf8 text\n"
    str = strings.TrimFunc(str, unicode.IsSpace)

    // The wrong way
    fmt.Printf("%s\n", str[0:len(str)/2])
    // The right way
    u8 := utf8string.NewString(str)
    FirstHalf := u8.Slice(0, u8.RuneCount()/2)
    fmt.Printf("%s\n", FirstHalf)

}

I am a still a GoLang newbie, so I'm not sure how the older experimental packages were integrated into the standard library. I did a bit of research and found that utf8string.NewString(str) is nowexpvar.NewString(str), so I changed my imports to

expvar
unicode

and modified the code appropriately to call expvar.NewString(str),but I'm still getting two errors:

u8.Slice undefined (type *expvar.String has no field or method Slice)
u8.RuneCount undefined (type *expvar.String has no field or method RuneCount)

I've tried a few different ways but can't seem to get it to work.

How should this example code be written for GoLang 1.2.1?

答案1

得分: 4

安装utf8string包:

$ go get -v code.google.com/p/go.exp/utf8string
code.google.com/p/go.exp (下载)
code.google.com/p/go.exp/utf8string
$

修复程序:

package main

import (
	"fmt"
	"strings"
	"unicode"

	"code.google.com/p/go.exp/utf8string"
)

func main() {
	str := "\tthe important rôles of utf8 text\n"
	str = strings.TrimFunc(str, unicode.IsSpace)

	// 错误的方式
	fmt.Printf("%s\n", str[0:len(str)/2])
	// 正确的方式
	u8 := utf8string.NewString(str)
	FirstHalf := u8.Slice(0, u8.RuneCount()/2)
	fmt.Printf("%s\n", FirstHalf)
}

输出:

the important r
the important rô

将程序修改为仅使用Go 1.2.1标准包:

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "\tthe important rôles of utf8 text\n"
	str = strings.TrimSpace(str)

	// 错误的方式
	fmt.Printf("%s\n", str[0:len(str)/2])
	// 正确的方式
	r := []rune(str)
	FirstHalf := string(r[:len(r)/2])
	fmt.Printf("%s\n", FirstHalf)
}

输出:

the important r
the important rô
英文:

Install package utf8string:

$ go get -v code.google.com/p/go.exp/utf8string
code.google.com/p/go.exp (download)
code.google.com/p/go.exp/utf8string
$

Fix the program:

package main

import (
	"fmt"
	"strings"
	"unicode"

	"code.google.com/p/go.exp/utf8string"
)

func main() {
	str := "\tthe important rôles of utf8 text\n"
	str = strings.TrimFunc(str, unicode.IsSpace)

	// The wrong way
	fmt.Printf("%s\n", str[0:len(str)/2])
	// The right way
	u8 := utf8string.NewString(str)
	FirstHalf := u8.Slice(0, u8.RuneCount()/2)
	fmt.Printf("%s\n", FirstHalf)
}

Output:

the important r
the important rô

Revising the program to only use Go 1.2.1 standard packages:

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "\tthe important rôles of utf8 text\n"
	str = strings.TrimSpace(str)

	// The wrong way
	fmt.Printf("%s\n", str[0:len(str)/2])
	// The right way
	r := []rune(str)
	FirstHalf := string(r[:len(r)/2])
	fmt.Printf("%s\n", FirstHalf)
}

Output:

the important r
the important rô

答案2

得分: 0

只需使用UTF8包(http://golang.org/pkg/unicode/utf8/)。

这个示例非常奇怪,因为没有必要调用"NewString"来创建一个UTF8字符串——在Go中,默认情况下所有的字符串都是UTF8的。

我建议找到另一个资源,因为这个短语手册似乎过时得误导人。

可以尝试使用"Go By Example"和"Way To Go"。

英文:

Just use the UTF8 package (http://golang.org/pkg/unicode/utf8/)

The example is extremely odd because there's no need to call "NewString" to create a UTF8 string—all strings in Go are UTF8 by default.

I'd suggest finding another resource since that Phrasebook seems so out of date as to be misleading.

Try "Go By Example" and "Way To Go".

huangapple
  • 本文由 发表于 2014年3月22日 15:34:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/22574814.html
匿名

发表评论

匿名网友

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

确定