Write your First Application (Hello World) with Go

  1. Create a folder anywhere you like and call it anything you want. I called it helloworld-basic and you can find it in this repository:
    https://github.com/gotutorial/helloworld.git
  2. Create a file with the extension of .go. You can give any name to that file. I named it main.go.
  3. Add following lines in main.go file
    package main
    
    import "fmt"
    
    func main() {
      fmt.Println("Hello World")
    }
  4. You can run your first Go Hello World application simply using go run command and you should be able to see “Hello World” text below your command execution.
    $ go run main.go
    Hello World
    
  5. You can also build an standalone executable file of your application using following command and run it.
    $ go build main.go

    You should be able to find an execution file with the same name main without any extension along with your main.go file:

    $ ls
    main  main.go
    

    And run the executable file like this:

    $ ./main
    Hello World
    

     

Leave a Reply