0% found this document useful (0 votes)
7 views42 pages

FMT Package GoLang

The document discusses the Go fmt package, including variadic functions, formatting verbs, and using fmt for input/output. It provides examples of using variadic parameters and arguments in functions, and exercises for readers to practice using fmt functions.

Uploaded by

thiagomatos.tlm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views42 pages

FMT Package GoLang

The document discusses the Go fmt package, including variadic functions, formatting verbs, and using fmt for input/output. It provides examples of using variadic parameters and arguments in functions, and exercises for readers to practice using fmt functions.

Uploaded by

thiagomatos.tlm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

fmt package

fmt package, variadic


fmt package
We have been using fmt.Println
http://godoc.org/fmt
let’s look at this package
What do these do?
what
do th
ese …
mea
n?
variadic
...params
args...
what does “…” mean in English?
it means there’s more, or there was more, or there was more here, doesn’t it?
example: The president said, “In these hard times … we all need to help.”
example: There were many options: cheese, wine, bread ….
example: … until the end of time.
https://golang.org/ref/spec#Function_types
https://golang.org/ref/spec#Function_types
exercise
create a program
that uses variadic params and variadic args
exercise
create a program
that uses a slice of ints param
and a slice of ints arg
fmt package
back to the fmt package
sa
w
th
is
%% just prints %
%v
examples of other verbs
https://raw.githubusercontent.com/GoesToEleven/GolangTraining/master/15_fmt-package/05_verbs/goByExample.go
os.Stdin
What is standard input for your
operating system?

F as in File
“... read from a specified io.Reader …”
io as in input output
exercise
create a program
that uses fmt.Scanln to receive keyboard input
does some calculation using that input
and then uses fmt.Println to display results
Review
● fmt package
○ Print
■ Print
■ Printf
■ Println
○ Scan
■ Scan
■ Scanf
■ Scanln
○ Sprint
■ Sprint
■ Sprintf
■ Sprintln
● variadic
○ func sum(numbers ...int) {
}
○ sum(aSlice...)
● verbs
○ %v
● receiving user input
var meters float64
fmt.Print("Enter meters swam: ")
fmt.Scanln(&meters)
● http://godoc.org/reflect
○ fmt.Println("a - ", reflect.TypeOf(a), " - ", a)
Review Questions
fmt package
● Describe the difference between Print, Printf, and Println
fmt package
● What are formatting verbs and how are they used?
● What is the default formatting verb?
fmt package
● What is the difference between Print, Fprint, & Sprint?
○ Read the documentation
■ describe how Sprint is used
● provide a screenshot of code you wrote to illustrate Sprint
■ speculate as to how Fprint might be used
○ What is an io.Writer
■ describe an io.Writer using quotes from the documentation
● take a screenshot of the io.Writer source code and include it
as a curio in your description
○ (a curio is something that you don’t understand yet but which is interesting)

click that to get to source code:


fmt package
● What is the difference between Scan, Fscan, & Sscan?
○ Read the documentation
■ describe how Sscan is used
● provide a screenshot of code you wrote to illustrate Sscan
■ speculate as to how Fscan might be used
○ What is an io.Reader
■ describe an io.Reader using quotes from the documentation
● take a screenshot of the io.Reader source code and include
it as a curio in your description
fmt package
● Describe what Sprint does.
○ Provide a screenshot of code you wrote to illustrate your description.
variadic
● Describe the difference between variadic params & variadic args.
○ Provide a screenshot of code you wrote to illustrate your description.

...params
args...

You might also like