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 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line