Skip to content

Instantly share code, notes, and snippets.

@kyokomi
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyokomi/adfc56e25eb6a9a99b30 to your computer and use it in GitHub Desktop.
Save kyokomi/adfc56e25eb6a9a99b30 to your computer and use it in GitHub Desktop.
UTF-8な日本語文字数を考慮したPrintf("%s")指定方法
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
hoges := []string{
"Hello, playground",
"ハロハロ, playground",
}
for _, hoge := range hoges {
textCount := 30 + ((utf8.RuneCountInString(hoge) - len(hoge)) / 2)
fmt.Println(textCount)
format := fmt.Sprintf("%%-%ds %%s\n", textCount)
fmt.Printf(format, hoge, "Go!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment