go run vs go build vs go install [3 best Tricks]

go run test.go:—

Go run vs go build vs go install, why so many ways to compile a go program. We will see the differences one by one.
To quickly test your go code and to check the output, you can use go run. But internally it compiles your code and builds an executable binary in a temporary location, launches that temp exe-file and finally cleans it when your app exits.
It looks very similar to how we run a script, like shell-script or a python code.
Also try “go help”, “go help build”, “go help run” and “go help install” on your terminal for more details.

sus@sus:~/src/mygo$ pwd
/home/sus/src/mygo
sus@sus:~/src/mygo$ ls
test.go
sus@sus:~/src/mygo$ cat test.go
package main
import “fmt”

func main() {
        fmt.Println(“Hello World!!”)
}
sus@sus:~/src/mygo$ go run //===== Line-X
go run: no go files listed
sus@sus:~/src/mygo$ go run test.go
Hello World!!
sus@sus:~/src/mygo$

Sometimes, to fix some error on our code we must modify the program number of times to check the output. In that case we can directly use go run, which will save lot of time.
One more thing to note, As in Line-X, “go run” always needs a source-file name(Here test.go).

go build:—

go build, compile and builds executable in current directory. It means, it will create an executable in current directory. Now If we want to run the program again, we do not have to compile it again, we simply run the executable file. Thus, it helps to reduce the compilation time. It means it helps to compile a program once and use the executable file anytime.

sus@sus:~/src/mygo$ pwd
/home/sus/src/mygo
sus@sus:~/src/mygo$ ls
test.go
sus@sus:~/src/mygo$ cat test.go
package main
import “fmt”

func main() {
        fmt.Println(“Hello World!!”)
}
sus@sus:~/src/mygo$
sus@sus:~/src/mygo$ go build  //====> Line-X
sus@sus:~/src/mygo$
sus@sus:~/src/mygo$ ls
mygo  test.go
sus@sus:~/src/mygo$ ./mygo
Hello World!!
sus@sus:~/src/mygo$ rm mygo
sus@sus:~/src/mygo$
sus@sus:~/src/mygo$ go build test.go  //====> Line-Y
sus@sus:~/src/mygo$ ls
test  test.go
sus@sus:~/src/mygo$ ./test
Hello World!!
sus@sus:~/src/mygo$

One more thing to notice, as in Line-X, if we just run “go build” without any source file name, then the executable will be build according to the current directory name(Here it is “mygo”). Else as in Line-Y, executable will be named according to source file-name (Here it is “test”).

go install:–

“go install” is like “go build”, but there is a difference. “go build” will compile and create the executable in current directory, whereas “go install” will compile and move the executable to $GOBIN directory, so that you can run this executable from any path on the terminal.
Executables are installed (Means copied) in the directory named by the GOBIN environment variable, which defaults to $GOPATH/bin. If the GOPATH environment variable is not set, default is $HOME/go/bin.

sus@sus:~/src/mygo$
sus@sus:~/src/mygo$ go env GOPATH
/home/sus
sus@sus:~/src/mygo$ go env GOBIN  //==== Line-1

sus@sus:~/src/mygo$
sus@sus:~/src/mygo$ rm -rf /home/sus/bin
sus@sus:~/src/mygo$
sus@sus:~/src/mygo$ ls /home/sus/bin    //==== Line-2
ls: cannot access ‘/home/sus/bin’: No such file or directory
sus@sus:~/src/mygo$
sus@sus:~/src/mygo$ pwd
/home/sus/src/mygo
sus@sus:~/src/mygo$ ls
hello.go
sus@sus:~/src/mygo$ cat hello.go
package main
import “fmt”

func main() {
        fmt.Println(“Hello World!!”)
}
sus@sus:~/src/mygo$
sus@sus:~/src/mygo$ go install  //==== Line-3
sus@sus:~/src/mygo$
sus@sus:~/src/mygo$ ls /home/sus/bin   //==== Line-4
mygo
sus@sus:~/src/mygo$ ls
hello.go
sus@sus:~/src/mygo$ /home/sus/bin/mygo    //==== Line-5
Hello World!!
sus@sus:~/src/mygo$

As in Line-1 to Line-4, if the GOBIN is not set, the executable is copied to default $GOPATH/bin location.  thus, here the “go install” will create a directory “/home/sus/bin” and will copy the executable to this location. Finally, in Line-5, we are able to execute the binary using the complete path.

The behavior is little different when you pass a source-file to “go install”, check below.

sus@sus:~/src/mygo$ go install hello.go  //==== Line-6
go install: no install location for .go files listed on command line (GOBIN not set)
sus@sus:~/src/mygo$
sus@sus:~/src/mygo$ export GOBIN=/home/sus/bin   //==== Line-7
sus@sus:~/src/mygo$
sus@sus:~/src/mygo$ go env GOBIN
/home/sus/bin
sus@sus:~/src/mygo$ go install hello.go  //==== Line-8
sus@sus:~/src/mygo$ ls /home/sus/bin    //==== Line-9
hello  mygo
sus@sus:~/src/mygo$ /home/sus/bin/hello
Hello World!!
sus@sus:~/src/mygo$ mygo   //==== Line-10
No command ‘mygo’ found, did you mean:
 Command ‘mago’ from package ‘mago’ (universe)
mygo: command not found
sus@sus:~/src/mygo$ export PATH=$PATH:/home/sus/bin    //==== Line-11
sus@sus:~/src/mygo$
sus@sus:~/src/mygo$ mygo   //==== Line-12
Hello World!!
sus@sus:~/src/mygo$ hello
Hello World!!
sus@sus:~/src/mygo$

As in Line-4, if we pass a source-file, like “go install hello.go”, it will check if the GOBIN env is set, else it will throw an error. Once we have set it as in Line-7, go install went fine and the exe hello(Here according to file name i,e hello.go) got generated at the location, as we have set in GOBIN.
Now to execute the exe (i,e hello or mygo) from any location in terminal we have to add the GOBIN to PATH as in Line-12.

15 thoughts on “go run vs go build vs go install [3 best Tricks]

  1. Have you ever considered about including a little bit more than just your articles?
    I mean, what you say is fundamental and all. But imagine if you added
    some great visuals or video clips to give your posts more, “pop”!
    Your content is excellent but with images and video clips, this site
    could certainly be one of the very best in its field.
    Wonderful blog!

  2. Excellent read, I just passed this onto a friend who was doing some research on that. And he actually bought me lunch as I found it for him smile So let me rephrase that: Thanks for lunch! Gill Jarret Garth

  3. Greate pieces. Keep posting such kind of info on your blog.
    Im really impressed by your site.
    Hello there, You have done a great job. I will definitely
    digg it and in my view suggest to my friends.
    I’m confident they will be benefited from this website.

  4. Hello there! This article couldn’t be written much better!
    Looking through this article reminds me of my previous roommate!
    He always kept preaching about this. I’ll forward this post to him.
    Pretty sure he will have a very good read. Thank you for sharing!

Leave a Reply

Your email address will not be published. Required fields are marked *