Programming/Easy Go

Setup Go development environments

EveryDayIsNewDay 2023. 9. 6. 16:09

Setup Go development environments

1. Install

Visit the official Go language(hereinafter Golang) website(https://go.dev) to download and install the appropriate program for your environment.

The installer will install Go to Program Files or Program Files (x86) by default. You can change the location as needed. After installing, you will need to reboot the computer so that changes to the environment made by the installer are reflected.

Verify installation as follows:

1) In Windows, click the Start menu.

2) In the menu's search box, type cmd, then press the Enter key

3) In the Command Prompt window that appears, type the following command

bash
닫기
go --version

4) Confirm that the command prints the installed version of Go

Verify Installation in Command Prompt

 

2. Write Simple Code

Let's print "Hello, World", often used when first learning a programming language.

 1) Use a command prompt to create a directory in which to write the sample program. In this example, "D:\Personal\Developement\hello" is specified.

2) Golang is still evolving. Sometimes, when a version is upgraded, some features are deprecated or merged. In addition, Golang can easily use functions (called modules) created by other people, but it is important to be able to check which version was used at the time of development. For this management, Golang creates and manages a file called go.mod.

To use a go.mod file to maintain dependencies on each module used in your code, run the "go mod init" command and specify the module name before writing the code. The module name must be the same as the module path. The reason for this is to ensure that the module is downloaded correctly when managing the module on github, etc. or when using it by others.

 

D:\Personal\Development\hello>go mod init development/hello
go: creating new go.mod: module development/hello
 
D:\Personal\Development\hello>type go.mod
module development/hello
 
go 1.21.0

 

You can see that the go.mod file is created through the Init command and the currently used golang version and module path are added. Now, when you write a program, the package information used in your go.mod file will be automatically added and updated.

Now let's launch the editor(There are many paid or free programs, but here we will use vscode, a free program from Microsoft) and write the following code(Numbers such as ‘01:’ in front of the code indicate the line number, so do not enter them):

 

1st programming in VSCode

go
닫기
package main import "fmt" func main() { ‌fmt.Println("Hello, World!") }

01: Declare a main package.  A package manages a group of functions and consists of all files located in the same directory. A Golang program starts with the main() function in the main package. This starting point is called the entry point.

03: Import the "fmt" package. The “fmt” package is a package that supports formatting when outputting strings, and is installed as a default library package when installing Golang.

05: Implement a main function to print a message to the console. A main function executes by default when you run the main package.

 

Type Golang's run command in the command prompt, you will see “Hello, World!”

 

D:\Personal\Development\hello>go run hello.go
Hello, World!

 

You can use the "build" command to create an executable file.

 

D:\Personal\Development\hello>go build hello.go
D:\Personal\Development\hello>dir/w
[.]         [..]        go.mod      hello.exe   hello.go
D:\Personal\Development\hello>hello
Hello, World!

 

'Programming > Easy Go' 카테고리의 다른 글

Using Conditional Statements: if / switch  (0) 2025.01.09
Operators(Arithmetic, Comparison, Logical)  (1) 2024.02.07
Print to Screen  (1) 2024.02.07
Variables and Constants  (0) 2023.09.08
About Go Language  (0) 2023.09.06