These executables are helpful to distribute, deploy, and test, but they cannot yet be executed from outside of their source directories. So far in this article, we have discussed how to generate executable binaries from our. Step 5 - Installing Go Programs with go install In order to use newly built executables from anywhere on your system, you can install them using go install. But with go build, you are still limited to running your binary from the current directory. You can now customize the name of your executable to fit the needs of your project, completing our survey of how to build binaries in Go. If you had a go.mod file in your project with a module declaration such as the following: Let’s take a closer look at the module method. When the go mod init greeter command was run, it created the module with the name ‘greeter’, which is why the binary generated is named greeter in turn. It does this by using the module you created earlier. When you run go build, the default is for Go to automatically decide on the name of the generated executable. Now that you know how to generate an executable, the next step is to identify how Go chooses a name for the binary and to customize this name for your project. In the next section, this tutorial will explain how a binary is named and how you can change it, so that you can have better control over the build process of your program. You can now distribute this program to new systems or deploy it to a server, knowing that the file will always run the same program. Now you have created a single executable binary that contains, not only your program, but also all of the system code needed to run that binary. If you are running macOS or Linux, you will find a new executable file that has been named after the directory in which you built your program: In this case, you built your greeter application into an executable file that was added to your current directory. go source files, or even a Go installation. It will also build all of the supporting code needed to be able to execute the binary on any computer with the same system architecture, regardless of whether that system has the. The command will include all your *.go files in the directory. If you do not provide an argument to this command, go build will automatically compile the main.go program in your current directory. In your greeter directory, run the following command: Using go build, you can generate an executable binary for our sample Go application, allowing you to distribute and deploy the program where you want. Step 3 - Building Go Binaries With go build Running it now will have no additional effect. Go will prompt you to run go mod tidy in order to update this module’s requirements if they change in the future. Outputgo: creating new go.mod: module greeter Once the file is open, add the following contents: Next, move into the newly created directory and create the main.go file in the text editor of your choice: To do this, you will use the classic “Hello, World!” program from the How To Write Your First Program in Go tutorial.Ĭreate a directory called greeter in your src directory: Step 1 - Setting Up and Running the Go Binaryįirst, create an application to use as an example for demonstrating the Go toolchain. A Go workspace set up by following How To Install Go and Set Up a Local Programming Environment.To follow the example in this article, you will need: In this tutorial, you will use the Go toolchain to run, build, and install a sample Hello, World! program, allowing you to use, distribute, and deploy future applications effectively. This is the same thing as installing the program onto your system. Putting these executables in an executable filepath on your own system will allow you to run the program from anywhere on your system. This means that a Go binary does not need system dependencies such as Go tooling to run on a new system. Once this executable is built, it will contain not only your application, but also all the support code needed to execute the binary on the target platform. In Go, the process of translating source code into a binary executable is called building. To do this, you can use the Go toolchain to build and install your program. Although this command is useful for testing your code on the command line, distributing or deploying your application requires you to build your code into a shareable binary executable, or a single file containing machine byte code that can run your application. So far in our How To Code in Go series, you have used the command go run to automatically compile your source code and run the resulting executable.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |