Belajar Pemrograman Go - Menampilkan Teks

3 min read 4 months ago
Published on Oct 19, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

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.

  1. Download Go

    • Visit the Go official download page.
    • Choose the installer appropriate for your operating system (Windows, macOS, or Linux).
  2. Install Go

    • Follow the instructions provided for your specific OS to complete the installation.
  3. 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.

  1. Open a Text Editor

    • Use any text editor (e.g., Visual Studio Code, Notepad++, or even Notepad).
  2. 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!")
      }
      
  3. Save the File

    • Ensure the file is saved with the .go extension.

Step 3: Compile the Go Program

To run your program, you need to compile it first.

  1. Open Command Prompt or Terminal

    • Navigate to the directory where your main.go file is saved using the cd command.
  2. Compile the Program

    • Run the following command:
      go build main.go
      
    • This command will generate an executable file named main (or main.exe on Windows).

Step 4: Run the Compiled Program

Now, you can execute your program and see the output.

  1. Execute the Program

    • In the Command Prompt or Terminal, run the following command:
      • On Windows:
        main.exe
        
      • On macOS/Linux:
        ./main
        
  2. 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.