Last active
August 29, 2015 14:06
-
-
Save kyokomi/adfc56e25eb6a9a99b30 to your computer and use it in GitHub Desktop.
UTF-8な日本語文字数を考慮したPrintf("%s")指定方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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