coding-to-music / coding-to-music.github.io

https://pandemic-overview.readthedocs.io/en/latest/index.html
MIT License
2 stars 8 forks source link

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

Open coding-to-music opened 3 years ago

coding-to-music commented 3 years ago

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

https://www.oldbloggers.com/go-run-vs-go-build-vs-go-install/

go run

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$
coding-to-music commented 3 years ago

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$ go build  //====> Line-X

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$ 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”).

coding-to-music commented 3 years ago

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.

coding-to-music commented 3 years ago

My changes

  294  go run install-validation.go

  295  go build
  296  go build install-validation
  297  go build install-validation.go

  298  go env GOPATH

  302  install-validation

  303  /home/tmc/bin/go/
  304  ls -l /home/tmc/bin/go/
  305  ls -l /home/tmc/bin/
  306  ls -l /home/tmc/go/
  307  ls -l /home/tmc/go/bin

  308  /home/tmc/go/bin/install-validation 

  309  go env GOBIN
  310  export PATH=$PATH:/home/tmc/bin
  311  export GOBIN=/home/tmc/bin
  312  go env GOBIN
  313  go env GOPATH
  314  go install install-validation
  315  install-validation

export PATH=$PATH:/home/tmc/bin

go env GOPATH
/home/tmc/go

export GOBIN=/home/tmc/bin

go env GOBIN
/home/tmc/bin
coding-to-music commented 3 years ago

Resulting changes

tmc@penguin:~/ap/install-validation$ go env GOPATH
/home/tmc/go

tmc@penguin:~/ap/install-validation$ go env GOBIN
/home/tmc/bin

tmc@penguin:~/ap/install-validation$ ls -l /home/tmc/bin
total 2276
-rwxr-xr-x 1 tmc tmc 2327484 Jun 21 16:41 install-validation

tmc@penguin:~/ap/install-validation$ ls -l /home/tmc/go
total 0
drwxr-xr-x 1 tmc tmc 106 Jun 21 16:29 bin
drwxr-xr-x 1 tmc tmc  16 Jun 18 15:38 pkg

tmc@penguin:~/ap/install-validation$ ls -l /home/tmc/go/bin
total 58420
-rwxr-xr-x 1 tmc tmc 14620302 Jun 19 14:31 dlv
-rwxr-xr-x 1 tmc tmc  3325699 Jun 18 16:08 go-outline
-rwxr-xr-x 1 tmc tmc  4433348 Jun 19 14:31 gopkgs
-rwxr-xr-x 1 tmc tmc 23116341 Jun 18 16:08 gopls
-rwxr-xr-x 1 tmc tmc  2327484 Jun 21 16:29 install-validation
-rwxr-xr-x 1 tmc tmc 11987135 Jun 19 14:31 staticcheck

tmc@penguin:~/ap/install-validation$ ls -l /home/tmc/go/pkg
total 0
drwxr-xr-x 1 tmc tmc 200 Jun 19 14:31 mod
drwxr-xr-x 1 tmc tmc  28 Jun 18 15:38 sumdb