From 1334ccbc9439ddbfc78e738384a75ce877936250 Mon Sep 17 00:00:00 2001 From: Baiju Muthukadan Date: Mon, 5 Jun 2023 11:00:52 +0530 Subject: [PATCH] Expand io chapter Signed-off-by: Baiju Muthukadan --- code/io/uppercase/uppercase.go | 19 ++++++++++++++++++ functions.tex | 3 ++- io.tex | 36 ++++++++++++++++++++++++---------- 3 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 code/io/uppercase/uppercase.go diff --git a/code/io/uppercase/uppercase.go b/code/io/uppercase/uppercase.go new file mode 100644 index 0000000..ec1f856 --- /dev/null +++ b/code/io/uppercase/uppercase.go @@ -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) +} diff --git a/functions.tex b/functions.tex index f032e11..1fa81af 100644 --- a/functions.tex +++ b/functions.tex @@ -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:} diff --git a/io.tex b/io.tex index 1209263..2007939 100644 --- a/io.tex +++ b/io.tex @@ -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} @@ -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: @@ -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