Can someone please explain

//Very new to Go and trying to understand why n holds the value 1.
//many thanks

func main() {
var y int
fmt.Println(y) //0
n,err:=fmt.Scanf("%d\n", &y)
if err != nil{
log.Fatal(err)
}
fmt.Println(“y =”,y)// prints the value of y
fmt.Println(“y+n =”,y+n) //some reason prints value of y + 1 where does the 1 come from? is n a bool?
fmt.Println(“n”,n) //1
fmt.Println(“y =”,y)

1 Like

n, err := fmt.Scanf("%d\n", &y)

func Scanf(format string, a ...interface{}) (n int, err error)

Scanf scans text read from standard input, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully scanned.

https://golang.org/pkg/fmt/#Scanf

One item - y - was successfully scanned.

1 Like

Thank you so much for explaining, very much appreciated.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.