Daily Exercise 004: Go-Golang-Tutorial-2-Your-First-Go-File
Day 1 Script
Let's start by creating our first simple Go file. We will call it main dot go, because that is a common name for the main application file. Every Go file you create must be part of a package, which is basically a collection of files and code. In our case, we will use package main at the start of the file. This tells the Go compiler that our code should be compiled into an executable program at the end. If we choose to build the application, Go will create a standalone executable file for us. Running that file will start our program automatically. If you were making a shared library instead, you would name your package something else to describe that library. Since we are making a program to run on our computer, we will always use package main for the entry file.
Day 1 Translate
| Japanese | English |
|---|---|
| あなたが作成するすべてのGoファイルはパッケージの一部である必要があります、基本的にはファイルとコードの集まりである | Every Go file you create must be part of a package, which is basically a collection of files and code. |
| これはGoコンパイラに伝えます、私たちのコードが最終的に実行可能なプログラムにコンパイルされるべきであることを | This tells the Go compiler that our code should be compiled into an executable program at the end. |
| 私たちはコンピュータで実行するプログラムを作っているので、エントリーファイルには常にパッケージメインを使用します | Since we are making a program to run on our computer, we will always use package main for the entry file. |
Day 2 Script
Next, we'll talk about the main function declaration. All functions in Go start with the func keyword. The name of this function is very important because it serves as the entry point of our application. When we run our program, Go looks through the files to find this specific function and executes it first. If you named it something else, Go would not fire it automatically when the program starts. It is also important to remember that there must be only one main function in the entire application. You do not need to have one in every single file within your package. You just have one main function in your entry file. In other files, you can create different functions and call them from inside this main function, but the entry point remains unique.
Day 2 Translate
| Japanese | English |
|---|---|
| この関数の名前は非常に重要です、なぜならそれが私たちのアプリケーションのエントリーポイントとして機能するからです | The name of this function is very important because it serves as the entry point of our application. |
| 私たちがプログラムを実行するとき、Goはファイルを検索してこの特定の関数を見つけ、それを最初に実行します | When we run our program, Go looks through the files to find this specific function and executes it first. |
| アプリケーション全体でメイン関数は1つだけでなければなりません | There must be only one main function in the entire application. |
Day 3 Script
Now we will look at how to import packages. We use the import keyword followed by the package name, such as fmt. This package is part of the Go standard library, which contains many useful tools for different tasks. The fmt package is specifically used for formatting strings and printing messages to the console. It contains several methods, and you might notice that they all start with a capital letter. This is because in Go, you must use a capital letter to export a function or method from a package. For example, we use the print line method to display text. We will learn more about formatting and printing later, but for now, just remember that we access these methods by typing the package name followed by a dot and the method name.
Day 3 Translate
| Japanese | English |
|---|---|
| このパッケージはGo標準ライブラリの一部です、様々なタスクのための多くの便利なツールを含んでいる | This package is part of the Go standard library, which contains many useful tools for different tasks. |
| fmtパッケージは特に使用されます、文字列のフォーマットやコンソールへのメッセージの表示のために | The fmt package is specifically used for formatting strings and printing messages to the console. |
| これはGoでは、パッケージから関数やメソッドをエクスポートするために大文字を使用する必要があるからです | This is because in Go, you must use a capital letter to export a function or method from a package. |
Day 4 Script
To run your Go program, you need to open a terminal and make sure you are in the correct directory. You can use a keyboard shortcut or the menu to open a new terminal window. Once you have verified your location, you can type go run followed by the name of your file, which is main dot go. When you hit enter, Go will automatically find and execute the main function we discussed earlier. You should see your message printed directly to the console. This simple command is a very quick way to test your code during development. It compiles and runs the program in one step, so you can see the results immediately. This process allows us to verify that everything is working correctly before we move on to more complex features like variables.
Day 4 Translate
| Japanese | English |
|---|---|
| Goプログラムを実行するために、ターミナルを開き、正しいディレクトリにいることを確認する必要があります | To run your Go program, you need to open a terminal and make sure you are in the correct directory. |
| このシンプルなコマンドは非常に素早い方法です、開発中にコードをテストするための | This simple command is a very quick way to test your code during development. |
| それは1つのステップでプログラムをコンパイルして実行します、その結果をすぐに確認できるように | It compiles and runs the program in one step, so you can see the results immediately. |
Day 5 Script
Variables are used to store information like strings and numbers. There are several ways to declare them in Go. The first way is to use the var keyword, followed by the variable name and its type. For example, you can declare a name as a string and set it equal to a value in double quotes. Go also allows you to omit the type, and it will infer it automatically based on the value you provide. Another very common shorthand is to use the colon equals operator, which initializes the variable without the var keyword. This is the method we will use most often inside functions. However, keep in mind that this shorthand cannot be used outside of a function. Whether you use explicit typing or inference, Go ensures that your variables maintain their type throughout the program.
Day 5 Translate
| Japanese | English |
|---|---|
| Goは可能にします、型を省略することを、そして提供された値に基づいてそれを自動的に推論することを | Go also allows you to omit the type, and it will infer it automatically based on the value you provide. |
| もう一つの非常に一般的な略記法は、コロンイコール演算子を使うことです、varキーワードなしで変数を初期化する | Another very common shorthand is to use the colon equals operator, which initializes the variable without the var keyword. |
| 明示的な型指定か推論かに関わらず、Goは保証します、プログラム全体で変数が型を維持することを | Whether you use explicit typing or inference, Go ensures that your variables maintain their type throughout the program. |