Belajar Pemrograman Go - Menampilkan Teks
Table of Contents
Introduction
This tutorial is designed to guide you through creating a simple program in the Go programming language (Golang) that displays text in the Command Prompt or Terminal. This fundamental exercise is an excellent starting point for beginners, as it helps you understand the basics of Go, including writing, compiling, and running your first program.
Step 1: Install Go
Before you can start programming, you need to have Go installed on your computer.
-
Download Go
- Visit the Go official download page.
- Choose the installer appropriate for your operating system (Windows, macOS, or Linux).
-
Install Go
- Follow the instructions provided for your specific OS to complete the installation.
-
Verify Installation
- Open your Command Prompt or Terminal.
- Type the following command and press Enter:
go version
- You should see the installed version of Go.
Step 2: Create a New Go File
Now that you have Go installed, it's time to write your first program.
-
Open a Text Editor
- Use any text editor (e.g., Visual Studio Code, Notepad++, or even Notepad).
-
Write the Go Code
- Start by creating a new file, and name it
main.go
. - Enter the following code to display text:
package main import "fmt" func main() { fmt.Println("Hello, World!") }
- Start by creating a new file, and name it
-
Save the File
- Ensure the file is saved with the
.go
extension.
- Ensure the file is saved with the
Step 3: Compile the Go Program
To run your program, you need to compile it first.
-
Open Command Prompt or Terminal
- Navigate to the directory where your
main.go
file is saved using thecd
command.
- Navigate to the directory where your
-
Compile the Program
- Run the following command:
go build main.go
- This command will generate an executable file named
main
(ormain.exe
on Windows).
- Run the following command:
Step 4: Run the Compiled Program
Now, you can execute your program and see the output.
-
Execute the Program
- In the Command Prompt or Terminal, run the following command:
- On Windows:
main.exe
- On macOS/Linux:
./main
- On Windows:
- In the Command Prompt or Terminal, run the following command:
-
View the Output
- You should see the text "Hello, World!" displayed in the terminal.
Conclusion
Congratulations! You've successfully created and run your first Go program. This exercise introduced you to the essential steps of installing Go, creating a program, compiling it, and executing it.
Next Steps
- Experiment by modifying the text in the
fmt.Println
function to display different messages. - Explore additional Go features such as variables, loops, and conditionals to expand your programming skills.