How do Print and Printf differ from each other in Go? - go

I am new to Go and understanding simple syntax and functions. Here I am confused between Print and Printf function. The output of those function is similar, so what is the difference between these two functions?
package main
import (
"fmt"
"bufio"
"os"
)
func main(){
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter Text: ")
str, _ := reader.ReadString('\n')
fmt.Printf(str)
fmt.Print(str)
}
I read https://golang.org/pkg/fmt/#Print to understand, but I did not understand it.

From the docs about Printing:
For each Printf-like function, there is also a Print function that takes no format and is equivalent to saying %v for every operand. Another variant Println inserts blanks between operands and appends a newline.
So Printf takes a format string, letting you tell the compiler what format to output your variables with and put them into a string with other information, whereas Print just outputs the variables as they are. Generally you'd prefer to use fmt.Printf, unless you're just debugging and want a quick output of some variables.
In your example you're sending the string you want to print as the format string by mistake, which will work, but is not the intended use. If you just want to print one variable in its default format it's fine to use Print.

Printf method accepts a formatted string for that the codes like "%s" and "%d" in this string to indicate insertion points for values. Those values are then passed as arguments.
Example:
package main
import (
"fmt"
)
var(
a = 654
b = false
c = 2.651
d = 4 + 1i
e = "Australia"
f = 15.2 * 4525.321
)
func main(){
fmt.Printf("d for Integer: %d\n", a)
fmt.Printf("6d for Integer: %6d\n", a)
fmt.Printf("t for Boolean: %t\n", b)
fmt.Printf("g for Float: %g\n", c)
fmt.Printf("e for Scientific Notation: %e\n", d)
fmt.Printf("E for Scientific Notation: %E\n", d)
fmt.Printf("s for String: %s\n", e)
fmt.Printf("G for Complex: %G\n", f)
fmt.Printf("15s String: %15s\n", e)
fmt.Printf("-10s String: %-10s\n",e)
t:= fmt.Sprintf("Print from right: %[3]d %[2]d %[1]d\n", 11, 22, 33)
fmt.Println(t)
}

As per docs
Print: will print number variables, and will not include a line break at the end.
Printf: will not print number variables, and will not include a line break at the end.
Printf is for printing formatted strings. And it can lead to more readable printing.
For more detail visit this tutorial.

Related

How to convert the string representation of a Terraform set of strings to a slice of strings

I've a terratest where I get an output from terraform like so s := "[a b]". The terraform output's value = toset([resource.name]), it's a set of strings.
Apparently fmt.Printf("%T", s) returns string. I need to iterate to perform further validation.
I tried the below approach but errors!
var v interface{}
if err := json.Unmarshal([]byte(s), &v); err != nil {
fmt.Println(err)
}
My current implementation to convert to a slice is:
s := "[a b]"
s1 := strings.Fields(strings.Trim(s, "[]"))
for _, v:= range s1 {
fmt.Println("v -> " + v)
}
Looking for suggestions to current approach or alternative ways to convert to arr/slice that I should be considering. Appreciate any inputs. Thanks.
Actually your current implementation seems just fine.
You can't use JSON unmarshaling because JSON strings must be enclosed in double quotes ".
Instead strings.Fields does just that, it splits a string on one or more characters that match unicode.IsSpace, which is \t, \n, \v. \f, \r and .
Moeover this works also if terraform sends an empty set as [], as stated in the documentation:
returning [...] an empty slice if s contains only white space.
...which includes the case of s being empty "" altogether.
In case you need additional control over this, you can use strings.FieldsFunc, which accepts a function of type func(rune) bool so you can determine yourself what constitutes a "space". But since your input string comes from terraform, I guess it's going to be well-behaved enough.
There may be third-party packages that already implement this functionality, but unless your program already imports them, I think the native solution based on the standard lib is always preferrable.
unicode.IsSpace actually includes also the higher runes 0x85 and 0xA0, in which case strings.Fields calls FieldsFunc(s, unicode.IsSpace)
package main
import (
"fmt"
"strings"
)
func main() {
src := "[a b]"
dst := strings.Split(src[1:len(src)-1], " ")
fmt.Println(dst)
}
https://play.golang.org/p/KVY4r_8RWv6

Split string on space (but not all..?)

Here I have a mock function that takes a single argument of multiple words in a single string, e.g. "hello1 hello2 hello3 hello4 hello5 hello6 hello7" etc.
The function will first do a match to see if the word content is in the string it received and then continue to split the string into arguments. If not matched with content then do something else.
My dosomething function is handling this data with the expectance of 5 arguments.
My question now is, how can I split these by delimiter space but everything that comes after hello5 should be part of arg5 below.
There is currently no way for me to know exactly how many arguments that are going to come in with mystring, hence concatenating fixed arguments will not work, it needs to be dynamic (is my assumption).
I hope this makes sense
func testing(mystring) {
matched, err := regexp.MatchString(`content`, mystring)
if err != nil { panic() }
if matched {
r := regexp.MustCompile("[^\\s]+")
arguments := r.FindAllString(clientRequest, -1)
arg1 := string(arguments[1])
arg2 := string(arguments[2])
arg3 := string(arguments[3])
arg4 := string(arguments[4])
arg5 := string(arguments[5])
dosomething(arg1, arg2, arg3, arg4, arg5)
} else {
log.Println("Not matched")
}
}
strings.SplitN does exactly what you want.
Here is a small demo:
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Printf("%q\n", strings.SplitN("a b c d e f g", " ", 5))
}
Output:
["a" "b" "c" "d" "e f g"]
I also suggest adding an additional if statement to check that strings.SplitN returns a slice of the correct length.
If you are sure you're searching for a word and not a regex pattern, you can use this:
exists := strings.Index(str, "content")
This will return the first index of occurrence of content, or return -1 if not found. If you do find a match, you can split the string till found index-1.
Here's a sample on playground to help you:
https://play.golang.org/p/QT39T6hStul

How do I count emojis in a string in go? [duplicate]

How can I get the number of characters of a string in Go?
For example, if I have a string "hello" the method should return 5. I saw that len(str) returns the number of bytes and not the number of characters so len("£") returns 2 instead of 1 because £ is encoded with two bytes in UTF-8.
You can try RuneCountInString from the utf8 package.
returns the number of runes in p
that, as illustrated in this script: the length of "World" might be 6 (when written in Chinese: "世界"), but the rune count of "世界" is 2:
package main
import "fmt"
import "unicode/utf8"
func main() {
fmt.Println("Hello, 世界", len("世界"), utf8.RuneCountInString("世界"))
}
Phrozen adds in the comments:
Actually you can do len() over runes by just type casting.
len([]rune("世界")) will print 2. At least in Go 1.3.
And with CL 108985 (May 2018, for Go 1.11), len([]rune(string)) is now optimized. (Fixes issue 24923)
The compiler detects len([]rune(string)) pattern automatically, and replaces it with for r := range s call.
Adds a new runtime function to count runes in a string.
Modifies the compiler to detect the pattern len([]rune(string))
and replaces it with the new rune counting runtime function.
RuneCount/lenruneslice/ASCII 27.8ns ± 2% 14.5ns ± 3% -47.70%
RuneCount/lenruneslice/Japanese 126ns ± 2% 60 ns ± 2% -52.03%
RuneCount/lenruneslice/MixedLength 104ns ± 2% 50 ns ± 1% -51.71%
Stefan Steiger points to the blog post "Text normalization in Go"
What is a character?
As was mentioned in the strings blog post, characters can span multiple runes.
For example, an 'e' and '◌́◌́' (acute "\u0301") can combine to form 'é' ("e\u0301" in NFD). Together these two runes are one character.
The definition of a character may vary depending on the application.
For normalization we will define it as:
a sequence of runes that starts with a starter,
a rune that does not modify or combine backwards with any other rune,
followed by possibly empty sequence of non-starters, that is, runes that do (typically accents).
The normalization algorithm processes one character at at time.
Using that package and its Iter type, the actual number of "character" would be:
package main
import "fmt"
import "golang.org/x/text/unicode/norm"
func main() {
var ia norm.Iter
ia.InitString(norm.NFKD, "école")
nc := 0
for !ia.Done() {
nc = nc + 1
ia.Next()
}
fmt.Printf("Number of chars: %d\n", nc)
}
Here, this uses the Unicode Normalization form NFKD "Compatibility Decomposition"
Oliver's answer points to UNICODE TEXT SEGMENTATION as the only way to reliably determining default boundaries between certain significant text elements: user-perceived characters, words, and sentences.
For that, you need an external library like rivo/uniseg, which does Unicode Text Segmentation.
That will actually count "grapheme cluster", where multiple code points may be combined into one user-perceived character.
package uniseg
import (
"fmt"
"github.com/rivo/uniseg"
)
func main() {
gr := uniseg.NewGraphemes("👍🏼!")
for gr.Next() {
fmt.Printf("%x ", gr.Runes())
}
// Output: [1f44d 1f3fc] [21]
}
Two graphemes, even though there are three runes (Unicode code points).
You can see other examples in "How to manipulate strings in GO to reverse them?"
👩🏾‍🦰 alone is one grapheme, but, from unicode to code points converter, 4 runes:
👩: women (1f469)
dark skin (1f3fe)
ZERO WIDTH JOINER (200d)
🦰red hair (1f9b0)
There is a way to get count of runes without any packages by converting string to []rune as len([]rune(YOUR_STRING)):
package main
import "fmt"
func main() {
russian := "Спутник и погром"
english := "Sputnik & pogrom"
fmt.Println("count of bytes:",
len(russian),
len(english))
fmt.Println("count of runes:",
len([]rune(russian)),
len([]rune(english)))
}
count of bytes 30 16
count of runes 16 16
I should point out that none of the answers provided so far give you the number of characters as you would expect, especially when you're dealing with emojis (but also some languages like Thai, Korean, or Arabic). VonC's suggestions will output the following:
fmt.Println(utf8.RuneCountInString("🏳️‍🌈🇩🇪")) // Outputs "6".
fmt.Println(len([]rune("🏳️‍🌈🇩🇪"))) // Outputs "6".
That's because these methods only count Unicode code points. There are many characters which can be composed of multiple code points.
Same for using the Normalization package:
var ia norm.Iter
ia.InitString(norm.NFKD, "🏳️‍🌈🇩🇪")
nc := 0
for !ia.Done() {
nc = nc + 1
ia.Next()
}
fmt.Println(nc) // Outputs "6".
Normalization is not really the same as counting characters and many characters cannot be normalized into a one-code-point equivalent.
masakielastic's answer comes close but only handles modifiers (the rainbow flag contains a modifier which is thus not counted as its own code point):
fmt.Println(GraphemeCountInString("🏳️‍🌈🇩🇪")) // Outputs "5".
fmt.Println(GraphemeCountInString2("🏳️‍🌈🇩🇪")) // Outputs "5".
The correct way to split Unicode strings into (user-perceived) characters, i.e. grapheme clusters, is defined in the Unicode Standard Annex #29. The rules can be found in Section 3.1.1. The github.com/rivo/uniseg package implements these rules so you can determine the correct number of characters in a string:
fmt.Println(uniseg.GraphemeClusterCount("🏳️‍🌈🇩🇪")) // Outputs "2".
If you need to take grapheme clusters into account, use regexp or unicode module. Counting the number of code points(runes) or bytes also is needed for validaiton since the length of grapheme cluster is unlimited. If you want to eliminate extremely long sequences, check if the sequences conform to stream-safe text format.
package main
import (
"regexp"
"unicode"
"strings"
)
func main() {
str := "\u0308" + "a\u0308" + "o\u0308" + "u\u0308"
str2 := "a" + strings.Repeat("\u0308", 1000)
println(4 == GraphemeCountInString(str))
println(4 == GraphemeCountInString2(str))
println(1 == GraphemeCountInString(str2))
println(1 == GraphemeCountInString2(str2))
println(true == IsStreamSafeString(str))
println(false == IsStreamSafeString(str2))
}
func GraphemeCountInString(str string) int {
re := regexp.MustCompile("\\PM\\pM*|.")
return len(re.FindAllString(str, -1))
}
func GraphemeCountInString2(str string) int {
length := 0
checked := false
index := 0
for _, c := range str {
if !unicode.Is(unicode.M, c) {
length++
if checked == false {
checked = true
}
} else if checked == false {
length++
}
index++
}
return length
}
func IsStreamSafeString(str string) bool {
re := regexp.MustCompile("\\PM\\pM{30,}")
return !re.MatchString(str)
}
There are several ways to get a string length:
package main
import (
"bytes"
"fmt"
"strings"
"unicode/utf8"
)
func main() {
b := "这是个测试"
len1 := len([]rune(b))
len2 := bytes.Count([]byte(b), nil) -1
len3 := strings.Count(b, "") - 1
len4 := utf8.RuneCountInString(b)
fmt.Println(len1)
fmt.Println(len2)
fmt.Println(len3)
fmt.Println(len4)
}
Depends a lot on your definition of what a "character" is. If "rune equals a character " is OK for your task (generally it isn't) then the answer by VonC is perfect for you. Otherwise, it should be probably noted, that there are few situations where the number of runes in a Unicode string is an interesting value. And even in those situations it's better, if possible, to infer the count while "traversing" the string as the runes are processed to avoid doubling the UTF-8 decode effort.
I tried to make to do the normalization a bit faster:
en, _ = glyphSmart(data)
func glyphSmart(text string) (int, int) {
gc := 0
dummy := 0
for ind, _ := range text {
gc++
dummy = ind
}
dummy = 0
return gc, dummy
}

What is the correct string format specifier for integer in fmt.Printf?

var count int = 5
fmt.Printf("count:%i\n", count)
Its output is
count:%!i(int=5)
What is the correct format specifier so that the output is
count:5
I look up the package fmt's method Printf in Go's package website, but it doesn't say about the syntax for a format specifier. Where can I find the syntax?
Thanks.
%d is the format specifier for base 10 integers (what you typically want) a full listing of fmt's format specifiers can be found here; https://golang.org/pkg/fmt/
var count int = 5
fmt.Printf("count:%d\n", count)
// prints count:5
%d is the format specifier for integer. However, You can use %v to print the value of the variable in default format, no matter what the data type is.
For example:
package main
import (
"fmt"
)
func main() {
//prints Hello 1 0.5 {Hello}
fmt.Printf("%v %v %v %v", "Hello", 1, 0.5, struct{ v string }{"Hello"})
}
You could also simply opt for the Println function:
fmt.Println("count:", count)

go - print without space between items

fmt.Println("a","b")
I want to print the two strings without space padding, namely "ab", but the above will print "a b".
Go fmt
Do I just switch to using Printf ?
fmt.Printf("%s%s\n","a","b")
Plain old print will work if you make the last element "\n".
It will also be easier to read if you aren't used to printf style formatting.
See here on play
fmt.Println("a","b")
fmt.Print("a","b","\n")
fmt.Printf("%s%s\n","a","b")
will print:
a b
ab
ab
As it can be found in the doc:
Println formats using the default formats for its operands and writes
to standard output. Spaces are always added between operands and a
newline is appended. It returns the number of bytes written and any
write error encountered.
So you either need to do what you already said or you can concatenate the strings before printing:
fmt.Println("a"+"b")
Depending on your usecase you can use strings.Join(myStrings, "") for that purpose.
Println relies on doPrint(args, true, true), where first argument is addspace and second is addnewline. So Prinln ith multiple arguments will always print space.
It seems there is no call of doPrint(args, false, true) which is what you want.
Printf may be a solution, Print also but you should add a newline.
You'd have to benchmark to compare performance, but I'd rather use the following than a Printf:
fmt.Println(strings.Join([]string{"a", "b"}, ""))
Remember to import "strings", and see strings.Join documentation for an explanation.
the solution in my project
package main
import "fmt"
var formatMap = map[int]string{
0: "",
1: "%v",
}
func Println(v ...interface{}) {
l := len(v)
if s, isOk := formatMap[l]; !isOk {
for i := 0; i < len(v); i++ {
s += "%v"
}
formatMap[l] = s
}
s := formatMap[l] + "\n"
fmt.Printf(s, v...)
}
func main() {
Println()
Println("a", "b")
Println("a", "b")
Println("a", "b", "c", 1)
}

Resources