Can Golang multiply strings like Python can? -


python can multiply strings so:

python 3.4.3 (default, mar 26 2015, 22:03:40) [gcc 4.9.2] on linux type "help", "copyright", "credits" or "license" more information. >>> x = 'my new text long' >>> y = '#' * len(x) >>> y '########################' >>> 

can golang equivalent somehow?

it has function instead of operator, strings.repeat. here's port of python example:

package main  import (     "fmt"     "strings"     "unicode/utf8" )  func main() {     x := "my new text long"     y := strings.repeat("#", utf8.runecountinstring(x))     fmt.println(y) } 

note i've used utf8.runecountinstring(x) instead of len(x); former counts "runes" (unicode code points), while latter counts bytes. in case of "my new text long", difference doesn't matter since characters 1 byte, it's habit of specifying mean.

(in python 2, len counts bytes on plain strings , runes on unicode strings (u'...'). in python 3, plain strings are unicode strings , len counts runes; if want count bytes, have encode string bytearray first. in go, there's 1 kind of string , don't have convert, have pick function matches semantics want.)


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -