Learn
Learn Go: fmt Package
Different Verbs

In addition to %v, Go has a variety of useful verbs (check their documentation for a comprehensive list). Let’s go over a few in this exercise, starting with %T:

specialNum := 42 fmt.Printf("This value's type is %T.", specialNum) // Prints: This value's type is int. quote := "To do or not to do" fmt.Printf("This value's type is %T.", quote) // Prints: This value's type is string.

As seen by the example above, the verb %T prints out the type of the second argument.

Now look at %d:

votingAge := 18 fmt.Printf("You must be %d years old to vote.", votingAge) // Prints: You must be 18 years old to vote.

Using %d we can interpolate a number into a string! If we need to include a float:

gpa := 3.8 fmt.Printf("You're averaging: %f.", gpa) // Prints: You're averaging 3.800000.

With %f, we can limit how precise we are by including a value between the % and f like: %.2f. If we include this in our code:

gpa := 3.8 fmt.Printf("You're averaging: %.2f.", gpa) // Prints: You're averaging 3.80.

Let’s explore these new verbs in our own code.

Instructions

1.

If you run the program without editing the given code, the first fmt.Printf() statement prints out: Working with a ____%!(EXTRA float64=1.75). You’ll also see similar statements for the other fmt.Printf() statements. That’s because there is an extra argument in each statement that is currently not used. This behavior will change as you progress through each step!

Edit the first fmt.Printf() statement to use %T and floatExample to print out Working with a float64..

2.

Edit the second fmt.Printf() statement and this time use 2 %d verbs in a string, yearsOfExp, and reqYearsExp to print out I have 3 years of Go experience and this job is asking for 15 years..

3.

Edit the third fmt.Printf() statement to use %f and stockPrice to print: Each share of Gopher feed is $3.50!.

Folder Icon

Take this course for free

Already have an account?