Daily Exercise 005: Go-Golang-Tutorial-4-Printing-Formatting-Strings

Day 1 Script

In this lesson, we are going to talk about the FMT package that we import in our Go files. This package is used to format strings and print them out to the console. We will go through a few different methods. You might have already seen the print line function. I am going to start with the print function. When we use the print function from the FMT package, we can pass in a string. It is important to remember that all of these methods start with a capital letter. This is because they are made public from the package. The print function is very similar to the print line function. However, there is one key difference. The print function does not add a new line at the end. We have to do it manually.

Day 1 Translate

Japanese English
このパッケージは使用されます、文字列をフォーマットし、それらをコンソールに出力するために This package is used to format strings and print them out to the console.
重要です、覚えておくことは、これらすべてのメソッドが頭文字で始まることを It is important to remember that all of these methods start with a capital letter.
プリント関数は末尾に改行を追加しません The print function does not add a new line at the end.

Day 2 Script

Next, let us look at the print line function again. This function does pretty much the same thing as the print function, but it adds a new line automatically at the end. This means we do not have to manually add a special character. It saves us a lot of time. Most developers use this function more often. Now, I want to show you that we can also output variables to the console. We can start with a string and then add a variable as a second argument. For example, we can say my age is, and then output the age variable. We can even output several different values. We just separate them with commas. This makes it very simple to print out a mix of text and stored values from our program.

Day 2 Translate

Japanese English
この関数はプリント関数とほぼ同じことを行いますが、末尾に自動的に改行を追加します This function does pretty much the same thing as the print function, but it adds a new line automatically at the end.
さて、お見せしたいと思います、変数もコンソールに出力できることを Now, I want to show you that we can also output variables to the console.
これは可能にします、テキストとプログラムからの保存された値を混ぜて出力することを、非常に簡単に This makes it very simple to print out a mix of text and stored values from our program.

Day 3 Script

Now, let us talk about formatted strings. These are a little bit more complicated. A formatted string is a way to create a single string with variables embedded directly inside it. This is often easier than breaking out of the string multiple times. To do this, we use the print f function. We pass in a string with format specifiers. A format specifier starts with a percentage sign and then a specific character. For example, we can use the letter V. This is the default format for most variables. When the function finds this specifier, it looks for the first variable we passed in. Then it outputs it at that position. We can also use the letter Q. This places double quotes around the variables. It is very useful when we want to clearly see the contents of a string variable.

Day 3 Translate

Japanese English
フォーマット済み文字列は方法です、変数がいわば直接埋め込まれた単一の文字列を作成するための A formatted string is a way to create a single string with variables embedded directly inside it.
これはしばしばより簡単です、異なる変数を追加するために文字列を何度も中断するよりも This is often easier than breaking out of the string multiple times.
フォーマット指定子は、パーセント記号とその後の特定の文字で始まります A format specifier starts with a percentage sign and then a specific character.

Day 4 Script

There are many different characters we can use with these format specifiers. If we use the percentage sign with the letter T, the function will output the type of the variable. This is very helpful for debugging. We can also use the letter F to output decimal numbers. In the Go language, these are known as floats. When we output a float, we might see many decimal points. However, we can limit the number of decimal points easily. We just put a number between the percentage sign and the letter F. For example, we can use a special decimal format to show only two points. This gives us great control over how our data appears. It looks much better on the console.

Day 4 Translate

Japanese English
Fという文字を使って、小数を出力することもできます We can also use the letter F to output decimal numbers.
単に数字を入れます、パーセント記号とFという文字の間に We just put a number between the percentage sign and the letter F.
これは与えてくれます、データがどのように表示されるかについての大きな制御を This gives us great control over how our data appears.

Day 5 Script

The final method I want to show you is called the sprint f function. This function is very similar to the print f function, but it does not directly print the output. Instead, it returns the formatted string. This allows us to store it in a variable. We can then use it later in our program. This is extremely useful when we need to build a complex message. It can then be sent to another part of the system. Once we have the saved string, we can use a standard print line function. We just output it whenever we are ready. We have covered the most common format specifiers. If we encounter any new ones later, I will explain them then. Next, we are going to learn about arrays and slices.

Day 5 Translate

Japanese English
この関数はプリントF関数と非常に似ていますが、出力を直接プリントしません This function is very similar to the print f function, but it does not directly print the output.
これは非常に便利です、複雑なメッセージを作成する必要があるときに This is extremely useful when we need to build a complex message.
いったん保存された文字列ができれば、標準的なプリントライン関数を使うことができます Once we have the saved string, we can use a standard print line function.