Leroy

How to Write 'Hello, World!' in Go

Leroy - Jun 1, 2024 - 2 min read

Go, also known as Golang, is a statically typed, compiled programming language designed for simplicity and efficiency. Writing a "Hello, World!" program is a great way to get started with Go. In this guide, we will walk you through the steps to write and run your first Go program.

Prerequisites

Before you begin, ensure you have the following:

  1. Go Installed: Download and install Go from the official website.
  2. A Code Editor: You can use any text editor, but Visual Studio Code is highly recommended for its Go extension.
  3. Command Line Access: You will need access to a terminal or command prompt to run your Go program.

Steps to Write "Hello, World!" in Go

  1. Create a New Directory

    Create a new directory for your Go project. For example:

    mkdir hello-world
    cd hello-world
    
  2. Initialize a Go Module

    Run the following command to initialize a new Go module:

    go mod init hello-world
    

    This will create a go.mod file, which is used to manage dependencies in your Go project.

  3. Create a New File

    Create a new file named main.go in your project directory. This will be the entry point of your Go program.

  4. Write the Code

    Open main.go in your code editor and add the following code:

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

    Explanation:

    • package main: Defines the package name. The main package is special because it defines a standalone executable program.
    • import "fmt": Imports the fmt package, which provides I/O functions like Println.
    • func main(): The main function is the entry point of the program. When you run the program, the code inside this function will execute.
    • fmt.Println("Hello, World!"): Prints the string "Hello, World!" to the console.
  5. Run the Program

    Open your terminal, navigate to the project directory, and run the following command:

    go run main.go
    

    You should see the following output:

    Hello, World!
    
  6. Build the Program (Optional)

    If you want to create an executable file, you can build your program using the go build command:

    go build main.go
    

    This will generate an executable file named main (or main.exe on Windows) in your project directory. You can run it directly:

    ./main
    

::: tip You can also run the program without creating an executable by using go run, which compiles and runs the code in one step. :::

Conclusion

Congratulations! You have successfully written and executed your first Go program. The "Hello, World!" program is a simple yet powerful way to get started with any programming language. From here, you can explore more advanced features of Go, such as working with functions, data structures, and concurrency.

Related Posts