Cool. That works. Just curious. Why do you write %d twice?
I think someone wrote you in the first question that you should read https://golang.org/pkg/fmt/ documentation
In short, you want to output 2 variables so you need two placeholders (of the correct type) for them.
Yes, definitely try reading the
fmt package documentation and see if you can understand it. It starts out describing
%d and other “verbs”, as they are called, that you can use in
fmt.Printf() (and similar) functions. They are very useful for doing basic input and output!
fmt.Printf() works with arguments somewhat like this:
fmt.Printf(“format string”, arg1, arg2, arg3 … ) // (not real Go code. Won’t compile.)
There must be one format string, and then there may be no additional arguments, or there can be a number of them. Verbs in the format string tell it how to print something, and the arguments supply what to print. Here are some simple examples:
fmt.Printf("hello, world\n") /// prints "hello, world" and a newline.
In that example, there are no verbs in the format string, and no additional arguments. You could do the same thing with
var name string = "Cherolyn" fmt.Printf("Hello, %s\n", name) // prints "Hello, Cherolyn" and a newline
%s, the verb for printing a string, is replaced by the value of
The verbs start with a percent sign (%) and are formatting directives. For every verb in the format string, you need to include an argument so
Printf() can insert its value into the string in the manner you specify.
Try your program again with
fmt.Printf("remainder for %d is %d\n",i,i%4)
Thanks! That makes sense. I had learned previously that %d referred to decimals, but “you want to output 2 variables so you need two placeholders” didn’t occur to me.
What here is the definition of “arguments”. I tried looking in the fmt package, language specification, and go doc; but I didn’t know where to find it.
I hate it when I run out of time!
Arguments are things passed to functions as inputs. Some examples will probably explain it best.
Println() function is supplied with one argument, a string with the value
sum = add(3,2)
add() has two arguments, integers
sum = add(3.67,two)
There are still two arguments, but the first is a floating-point number (
3.67),and the second is a variable (
max_value = max(a,b,f(c),"name",'$')
max() has five arguments: variables
b, the return value of function
f(), a string (
"name"), and a rune (the dollar sign character).
Interesting definition of arguments. I think I got it.
I accidentally deleted a part of your answer that I wanted to comment on. It was the code you made that resulted in Hello Cherolyn… That was a fun code.
Oh it’s still there.
With the understanding of arguments, I went back to your previous response, and I got it! Fun!
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.