This article only as a personal notes, reference to: blog.csdn.net/zhangliangz…

├── heavy exercises ─ heavy exercises ─ heavy exercisesCopy the code

hello.go

package mypkg

func Hello(a) string {
	return "hello"
}
Copy the code

test.go

package main

import (
	"fmt"
	"mypkg"
)

func main(a) {
	fmt.Println(mypkg.Hello())
}

Copy the code

CD Go to the mypkg directory

  • go buildThere is no effect
  • go installIn the directory$GOPATH/pkg/$GOOS_$GOARCHNext generationmypkg.a

CD to myapp directory

  • go buildGets the executable in the current directorymyapp
  • go build test,goGets the executable in the current directorytest
  • go installIn the directory$GOPATH/binGenerate the executable filemyapp
  • go install test.goIn the directory$GOPATH/binGenerate the executable filetest

Go Build builds the executable file in the current directory and calls all source code references to the package, recompiling instead of using the compiled file directly in the PKG. If the project source code of the import import package is not found under $GOROOT and $GOPATH, an error will be reported. If I delete $GOPATH/ SRC /mypkg and execute the go build command, I will get an error.

Go install Compiles the source code. If it is an executable (package “main” and contains the main method), it will compile and generate the executable file to the $GOPATH/bin directory. Other packages introduced by the executable import are compiled into the $GOPATH/ PKG /$GOOS_$GOARCH directory.

Go Install can also do a similar function to Go Build, except that go Install generates a binary file in the $GOPATH/ PKG /$GOOS_$GOARCH directory (this binary file is not useful to us). Go Install, however, automatically generates the executable into the $GOBIN directory

Summary: All the same can generate executable files

Different points Go build cannot generate package files. Go install can generate package files. Go build generates executable files in the current directory. Go install Generates executable files in the bin directory ($GOPATH/bin) go Build is often used to compile tests. Go Install is mainly used for production libraries and tools.

—— add —— go build: Compile an executable file go install: go build + Put the compiled executable file in the GOPATH/bin directory go get: git clone + go install