需要使用“exp / utf8string”来翻译代码到后来的标准库代码中 [英] Need to translate code using "exp/utf8string" into later standard library code

查看:126
本文介绍了需要使用“exp / utf8string”来翻译代码到后来的标准库代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从 Go编程语言短语手册 - 该书于2012年编写,基于Go 1.0。该示例使用 exp / utf8string 包,该包现在已变为 unicode / utf8 。由于 exp / utf8string 包现在不存在,因此我正在使用Go 1.2.1,下面列出的代码不会按原样编译:

  package main 
importstrings
importunicode
importexp / utf8string
importfmt

func main()
{
str:=\tthe utf8 text\\\

str = strings.TrimFunc(str, unicode.IsSpace)

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

}

我仍然是GoLang新手,所以我不确定旧的实验软件包是如何集成到标准库中的。我做了一些研究,发现 utf8string.NewString(str)现在是 expvar.NewString(str),所以我改变了我的进口到

  expvar 
unicode

,然后修改代码以调用 expvar.NewString(str),但我仍然收到两个错误:

  u8.Slice undefined(type * expvar.String没有字段或方法Slice)
u8.RuneCount undefined (类型* expvar.String没有字段或方法RuneCount)

我尝试了几种不同的方法但似乎无法得到它的工作。

这个示例代码应该如何写入GoLang 1.2.1?

安装软件包 utf8string

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

解决方案:

<$ p
$ f











$ b $ code.google.com/p/go.exp/utf8string


func main(){
str:=\tthe utf8 text \\ \\ n
str = strings.TrimFunc(str,unicode.IsSpace)

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

输出:

 重要的r 
重要的rô

修改程序只使用Go 1.2.1标准包:

  package main 














$ b $ str:= \ tttf8 text \\\

str = strings.TrimSpace(str)

//错误的方法
fmt.Printf(%s\ (str)$ str $ {$ str $ str $ [$ str(str)$ str $ str $ str $ str $ [ r)/ 2])
fmt.Printf(%s \ n,FirstHalf)
}

输出:

 重要的r 
重要的rô
code>


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?

解决方案

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ô

这篇关于需要使用“exp / utf8string”来翻译代码到后来的标准库代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆