Sunday, May 19, 2024
HomeGolangWhy does a % signal present up once I print a float?...

Why does a % signal present up once I print a float? – Getting Assist


Hello, I’m on a MacBook Professional M1 chipset laptop computer. As proven under, I get a “%” signal printed once I don’t use “n”, however once I do use “n”, It prints with out the “%”.

chap3 % cat important.go
bundle important

import "fmt"

func important() {
	fmt.Printf("%g", 2.332)
}
chap3 % go run important.go
2.332%
chap3 % vim important.go
chap3 % cat important.go
bundle important

import "fmt"

func important() {
	fmt.Printf("%gn", 2.332)
}
chap3 % go run important.go
2.332
chap3 % go model
go model go1.19.2 darwin/arm64

Am I doing one thing fallacious or lacking one thing?

Hello, Please ignore the above query. The % was inserted by zsh. After I change over to bash, I get the anticipated output.

bash-3.2$ go run important.go
2.332bash-3.2$ vim important.go
bash-3.2$ go run important.go
2.332
bash-3.2$

Hello, @pmu, welcome again to the discussion board!

It is a nice query. Because it seems, like you’ve got already discovered when testing completely different shells, that is an artifact of utilizing zsh. If a program doesn’t insert a closing newline, zsh will insert the % signal as a sign to the person that this system ended with out a newline. bash doesn’t do this.

This occurs as a result of fmt.Printf("%g", 2.332) doesn’t embrace a brand new line character. For that, it’s important to use fmt.Printf("%gn", 2.332).

I’m not conversant in shells aside from bash, so it’s good to know that we should always maintain an eye fixed out for points like this which may be brought on by different shells like zsh. Thanks for asking and for posting the reply; possibly if another person has this problem sooner or later, you’ll save them some headache!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments