Skip to content

Commit

Permalink
Expand io chapter
Browse files Browse the repository at this point in the history
Signed-off-by: Baiju Muthukadan <baiju.m.mail@gmail.com>
  • Loading branch information
baijum committed Jun 5, 2023
1 parent 4ffda83 commit 1334ccb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
19 changes: 19 additions & 0 deletions code/io/uppercase/uppercase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
"io"
"os"
"strings"
)

func main() {
stdin, err := io.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
str := string(stdin)
newStr := strings.TrimSuffix(str, "\n")
upper := strings.ToUpper(newStr)
fmt.Println(upper)
}
3 changes: 2 additions & 1 deletion functions.tex
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,8 @@ \section{Methods}

\section{Exercises}

\textbf{Exercise 1:} Write a method to calculate the area of a rectangle.
\textbf{Exercise 1:} Write a method to calculate the area of a rectangle for a
given struct with width and height.

\textbf{Solution:}

Expand Down
36 changes: 26 additions & 10 deletions io.tex
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,31 @@ \section{Standard Streams}

As you can see from this program, the \textit{Printf} function writes to
standard output and the \textit{Scanf} reads the standard input. Go can also
writes to standard error output stream. This is explained in the section about
the \textit{io} package later in this chapter.
writes to standard error output stream.

The \textit{io} package provides a set of interfaces and functions that allow
developers to work with different types of input and output streams.

Consider a use case to convert everything that comes to standard input to
convert to upper case. This can be achieved by reading all standard input
using \texttt{io.ReadAll} and converting to upper case. Here is code:

\lstinputlisting[caption=Convert standard input to upper case]{code/io/uppercase/uppercase.go}

You can run this program similar to how you did with the previous program.

You can use \textit{fmt.Fprintf} with \textit{os.Stderr} as the first argument
to write to standard error.

\begin{lstlisting}[numbers=none]
fmt.Fprintf(os.Stderr, "This goes to standard error: %s", "OK")
\end{lstlisting}

Alternatively, you can call \textit{WriteString} method of \textit{os.Stderr}:

\begin{lstlisting}[numbers=none]
os.Stderr.WriteString("This goes to standard error")
\end{lstlisting}

\section{Using flag Package}

Expand Down Expand Up @@ -279,7 +302,7 @@ \section{String Formatting}
printed will be whatever returned by that function. Here is an
example:

\lstinputlisting[caption=Representation format]{code/io/custom/custom.go}
\lstinputlisting[caption=Custom representation using Stringer]{code/io/custom/custom.go}

The output of the above program will be like this:

Expand All @@ -290,13 +313,6 @@ \section{String Formatting}
main.Temperature{Value:30.456, Unit:"Celsius"}
\end{lstlisting}

\section{Using io Package}

The \textit{io} package provides a set of interfaces and functions that allow
developers to work with different types of input and output streams.

TODO

\section{Exercises}

{\bfseries Exercise 1:} Write a program to read length and width of a
Expand Down

0 comments on commit 1334ccb

Please sign in to comment.