ma6254
V2EX  ›  问与答

Go 语言新手求教 | 字符串格式化的中文对齐问题

  •  
  •   ma6254 · Apr 11, 2018 · 4187 views
    This topic created in 2976 days ago, the information mentioned may be changed or developed.

    本人是 golang 新手,遇到个问题,求教各位大佬
    example
    online
    如上所示,fmt.Printf 在计算字符串长度的时候应该是以 Rune 为单位的
    但是在控制台下英文占一个位置,中文占两个位置
    求教如何解决,挺基础的问题,求轻喷

    3 replies    2018-04-11 19:13:34 +08:00
    dontwanttologin
        1
    dontwanttologin  
       Apr 11, 2018
    https://github.com/golang/go/issues/7481 这个 issue 里的回复,用 go/printer 好像不能解决这个问题。
    BGLL
        2
    BGLL  
       Apr 11, 2018
    用等宽字体
    hcwhan
        3
    hcwhan  
       Apr 11, 2018
    可以试下这个
    https://github.com/mattn/go-runewidth
    自己封装一个加空格的函数
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3412 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 11:08 · PVG 19:08 · LAX 04:08 · JFK 07:08
    ♥ Do have faith in what you're doing.