douchunsui2395 2014-03-22 07:34
浏览 96
已采纳

需要使用“ exp / utf8string”将代码转换为更高的标准库代码

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
"
    str = strings.TrimFunc(str, unicode.IsSpace)

    // The wrong way
    fmt.Printf("%s
", str[0:len(str)/2])
    // The right way
    u8 := utf8string.NewString(str)
    FirstHalf := u8.Slice(0, u8.RuneCount()/2)
    fmt.Printf("%s
", 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?

  • 写回答

2条回答 默认 最新

  • dongwen5019 2014-03-22 08:46
    关注

    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
    "
        str = strings.TrimFunc(str, unicode.IsSpace)
    
        // The wrong way
        fmt.Printf("%s
    ", str[0:len(str)/2])
        // The right way
        u8 := utf8string.NewString(str)
        FirstHalf := u8.Slice(0, u8.RuneCount()/2)
        fmt.Printf("%s
    ", 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
    "
        str = strings.TrimSpace(str)
    
        // The wrong way
        fmt.Printf("%s
    ", str[0:len(str)/2])
        // The right way
        r := []rune(str)
        FirstHalf := string(r[:len(r)/2])
        fmt.Printf("%s
    ", FirstHalf)
    }
    

    Output:

    the important r
    the important rô
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的