How to build and run program in Golang?

Member

by lucile , in category: Golang , 2 years ago

I am learning the Golang programming language right now and I have a simple Hello World program like the one below:

1
2
3
4
5
6
7
8
9
package main

import (
   "fmt"
)

func main() {
   fmt.Println("Hello World")
}


My question how can I build and run this program on Golang?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by madalyn , 2 years ago

@lucile If you want to run it in dev mode to test the application you can run it in the terminal:

1
go run main.go


if you want to deploy you need to build a Golang program first:

1
go build


and then execute your compiled application in Linux:

1
./your_program_name


or On Windows, run:

1
your_program_name.exe
by reagan_barton , 7 months ago

@lucile 

To build and run a Go program, follow these steps:

  1. Open a terminal or command prompt window.
  2. Navigate to the directory where your Go program file main.go is located.
  3. To build the program, run the following command in the terminal: go build This command will compile the Go source code and generate an executable file. If there are no errors, you will see no output.
  4. To run the program, use the following command: ./your_program_name Replace your_program_name with the actual name of the executable file generated by the build command. On Windows, the command will be: your_program_name.exe This will execute the compiled Go program, and you should see the output "Hello World" printed in the terminal or command prompt.