How to use the GO INSTALL command

Want to run a Go program as a complete executable binary? The Go install command compiles and installs the application in the workspace's bin directory. Methods as below.

Translated from Golang: How To Use the Go Install Command , author Jack Wallen.

The Go language has a special command that compiles and installs an application's binary package into a path accessible to the application's users.

Let me explain it in a way we can all understand.

First, let's talk about PATH. PATH is a special list of directories that instructs the system where to find the executable files needed to run a command. For example: For Linux, run the command...

echo $PATH

You might see something like this in the output:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Essentially, this means that any executable file located in these directories can be run from anywhere in the file system hierarchy. Thanks to PATH, you don't need to use the full path for commands like ls, which is:

/usr/bin/ls

Instead, you can simply run ls to use the application.

When you install Go, it assumes that Go's path defaults to a specific location. To find out where that path is, issue the command:

echo $GOPATH

You should see something like this:

/home/jack/go/

You can set this path using the following command:

go env -w GOPATH=$HOME/go

Note that $HOME is equivalent to _/home/USER_ (where _USER_ is your Linux username). You can also set it in the .bash_profile file. Open the file for editing using the following command:

nano ~/.bash_profile

At the bottom of the file, add the following:

export GOPATH=$HOME/go

Use the following command to get the file:

source ~/.bash_profile

You can change this path if you want, but it's best to always leave it as is (especially when starting out).

Okay, now you know what GOPATH is and how to use it?

let me tell you.

Let's write a program to calculate an approximation of pi. Here's how this app works:

  1. Import packages fmt , math and math/rand .
  2. Set the random number generator seed, setting _totalPoints_ to 1 million and _pointsInsideCircle_ to 0.
  3. Use a for loop to iterate over _totalPoints_, set both x and y to a random floating point number of 64, and multiply x_x and y_y using these numbers (using the _math.Sqrt_ function).
  4. Set _piApprox_ to 4 times the float64 of _pointsInsideCircle_ and _totalPoints_.
  5. Finally, print out the value.

Here is the code:

package main

import (
        "fmt"
        "math"
        "math/rand"
)

func main() {
        rand.Seed(42)
        totalPoints := 1000000
        pointsInsideCircle := 0

        for i := 0; i < totalPoints; i++ {
                x := rand.Float64()
                y := rand.Float64()

                distance := math.Sqrt(x*x + y*y)

                if distance < 1 {
                        pointsInsideCircle++
                }
        }

        piApprox := 4 * float64(pointsInsideCircle) / float64(totalPoints)
        fmt.Printf("Using this method, our approximate value of π: %f\n", piApprox)

}

Create a new project directory using the following command:

mkdir ~/randompi

Change to this directory using the following command:

cd randompi

Initialize the project using the following command:

go mod init randompi

Create the main.go file using the following command:

nano main.go

Paste the code into this file and save it.

Build the application using the following command:

go build

You should now see a binary executable named _randompi_. You can run a new Go application using the following command:

./randompi

marvelous. But what if you want to be able to run the command from a different directory? Since this is Linux, you could copy it to the /usr/local/bin directory, but Go already provides a GOPATH for this purpose. To do this, you can use go install, which will move the new binary into your GOPATH. To do this, issue the following command:

go install

If you issue the ls command, you will find that the _randompi_ executable is now gone. Where did it go? Go has moved it into your GOPATH. Remember to list your GOPATH using:

echo $GOPATH

This should print out your GOPATH. The trick here is that Go doesn't just copy the executable to the root of your GOPATH. Instead, it copies it to the bin directory in that path. In Linux terminology, bin is a common directory for binary files (bin = binary). To verify that the executable file has been copied to the path, issue the following command:

ls $GOPATH/bin

You should see _randompi_ listed.

If you know Linux , you probably already understand what happens next. Even if you have set GOPATH, it does not mean it is in your Linux PATH. Even with this warning, Go has you covered with the run command. If you issue the following command:

go run randompi

It will find the binary executable in $GOPATH/bin and run the randompi application, its output will be similar to:

Using this method, our approximate value of π: 3.139740

Here's a little trick.

When you initialize your application using go mod init randompi, it will create a go.mod file that will contain something like:

module randompi

go 1.22.1

Let's say you want to rename your application to gopi. All you have to do is edit the go.mod file and change module randompi to module gopi. Rebuild and reinstall the application, then you can run the application using:

go run gopi

That's it my Go friends , go installthe basics of using commands. This will become an important tool for you as you continue to learn the Go language .

This article was first published on Yunyunzhongsheng ( https://yylives.cc/ ), everyone is welcome to visit.

The Google Python Foundation team was laid off. Google confirmed the layoffs, and the teams involved in Flutter, Dart and Python rushed to the GitHub hot list - How can open source programming languages ​​and frameworks be so cute? Xshell 8 opens beta test: supports RDP protocol and can remotely connect to Windows 10/11. When passengers connect to high-speed rail WiFi , the "35-year-old curse" of Chinese coders pops up when they connect to high-speed rail WiFi. MySQL's first long-term support version 8.4 GA AI search tool Perplexica : Completely open source and free, an open source alternative to Perplexity. Huawei executives evaluate the value of open source Hongmeng: It still has its own operating system despite continued suppression by foreign countries. German automotive software company Elektrobit open sourced an automotive operating system solution based on Ubuntu.
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/6919515/blog/11080487