Three almost similar go commands

| Reading Time : 2 minutes | #backenddevelopment #Go


Go has three variations of similar commands go run, go build, and go install. We will see in this story what is the purpose of each command and how to use them.

Each of these commands either takes a single Go file or a list of Go files. Here in this story, we will use a Go hello world program for example to refer along the way.

// file name : hello.go
package main
import "fmt"
func main(){
   
     fmt.Println("Hello, world!")
}

go run

If you run the command go run hello.go in your terminal/command prompt you should see the output as Hello, world! printed.

What happens when you run this command is Go compiles the code into a binary file, But places this file in a temporary directory and executes this binary file from there, and deletes it after your program finishes. This command is useful for testing small programs during the initial development stage of your application.

go build

So now you want to you have the binary you built for the application to use later or run this binary on a remote computer then you need to use go build command as below which creates a binary file named hello in the current directory(hello.exe in windows)

go build hello.go

Tip: If you want to change the name of the binary that generated use a file -o

// This results a binary file "application"
go build -o application hello.go

go install

This command does perform the exact operation as go build but places the binary in $GOPATH/bindirectory alongside the binaries of third-party tools installed viago get now if you run$GOPATH/bin/helloyou will seeHello, world!` printed on your terminal.

Suggestion when using go install is to use this if you are planning to write tools that you want to use on your own computer or usego build if you are to run this application in elsewhere


Articles from blogs I follow around the net

powerctl: A small case study in Hare for systems programming

powerctl is a little weekend project I put together to provide a simple tool for managing power states on Linux. I had previously put my laptop into suspend with a basic “echo mem | doas tee /sys/power/state”, but this leaves a lot to be desired. I have to u…

via Drew DeVault's blog August 28, 2022

United States v. Microsoft Corp exhibits

Links to exhibits from the Microsoft anti-trust case, with a bit of info on each link. Projection of PC marketshare Share of new browser users Share of the browser market, grouped by major ISP, 3 month moving average Share of the browser market, grouped by ma…

via danluu.com August 24, 2022

Status update, August 2022

Hi all! This month I’ve been pondering offline-first apps. The online aspect of modern apps is an important feature for many use-cases: it enables collaboration between multiple people and seamless transition between devices (e.g. I often switch between my pe…

via emersion August 14, 2022

Generated by openring

© Copyright 2021-2022. Rakesh Mothukuri