This is the third day of my participation in the August Text Challenge.More challenges in August
The problem
To solve
At the end
The problem
When using Golang to write a program, we often encounter a single file compilation run scenario, when we use the general compilation statement to compile the situation often error, exactly what is the problem? Next, let’s analyze it with specific examples.
To solve
Here’s Golang’s simplest “Hello, world” code:
package main
import "fmt"
func main(a) {
fmt.Println("Hello, World!")}Copy the code
Save the above code to the helloworld.go file. When we want to compile and run the file, we can use the following command to do so:
go run helloworld.go
So, the program will print:
Hello, World!
Screenshot below:
As you can see from the above example, when executing a Golang file, it’s easy to do in a single command.
However, when unofficial base libraries are introduced into the Golang file, the problem becomes more complicated. The above code example is fine because the FMT package is Golang’s system library (system toolkit).
Let’s look at an example.
Here is an example of code that introduces an unofficial third library:
package main
import (
"github.com/minio/minio-go/v6"
"fmt"
)
func main(a) {
endpoint := "min.io.test"
accessKeyID := "Q3AM3UQ867SPQQA43P2F"
secretAccessKey := "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"
useSSL := true
// Initialize the minio client object.
minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
iferr ! =nil {
fmt.Println("Program execution exception, error :", err.Error())
}
fmt.Println("Program completed")}Copy the code
The import statement of this code introduces a third party library “github.com/minio/minio-go/v6”. We will execute this code again with the command above, save the file as testminio.go, and execute the following command:
go run testMinio.go
The error message is as follows:
Why can’t I compile the same command here?
The reason is that the first code uses the system library, Golang compiler can definitely recognize, but the second code uses the third library, Golang compiler did not find in the system, so the error is reported, how should we solve?
To import the third library into the local Golang environment variable path, run the following command:
go get github.com/minio/minio-go/v6
Normally, the minio-related folder is generated in the go/src/github.com directory. If the -v -u parameter cannot be added, run the following command:
go get -v -u github.com/minio/minio-go/v6
Go to the corresponding directory and run the following command:
go mod init
go mod tidy
The following figure shows the result:
Then execute the above compilation command.
At the end
I am liuzhen007, a Chinese Bond, a Chinese code-type Bond. I am liuzhen007, a Chinese code-type Bond.