Go Cli Binary Not Being Generated For Mac
Introduction The comes with a rich toolchain that makes obtaining packages and building executables incredibly easy. One of Go's most powerful features is the ability to cross-build executables for any Go-supported foreign platform. This makes testing and package distribution much easier, because you don't need to have access to a specific platform in order to distribute your package for it. In this tutorial, you'll use Go's tools to obtain a package from version control and automatically install its executable. Then you'll manually build and install the executable so you can be familiar with the process.
Then you'll build an executable for a different architecture, and automate the build process to create executables for multiple platforms. When you're done, you'll know how to build executables for Windows and macOS, as well as other platforms you want to support. Prerequisites To follow this tutorial, you will need:. One Ubuntu 16.04 server set up by following, including a sudo non-root user and a firewall. Go installed, as described in. Step 1 — Installing Go Programs from Version Control Before we can create executables from a Go package, we have to obtain its source code.
The go get tool can fetch packages from version control systems like GitHub. Under the hood, go get clones packages into subdirectories of the $GOPATH/src/ directory. Then, if applicable, it installs the package by building its executable and placing it in the $GOPATH/bin directory. If you configured Go as described in the prerequisite tutorials, the $GOPATH/bin directory is included in your $PATH environmental variable, which ensures that you can use installed packages from anywhere on your system. The syntax for the go get command is go get package-import-path. The package-import-path is a string that unique identifies a package.
It's often the location of the package in a remote repository like Github, or a directory within the $GOPATH/src/ directory on your machine. It's common to use go get with the -u flag, which instructs go get to obtain the package and its dependencies, or update those dependencies if they're already present on the machine. In this tutorial, we will install, a web server written in Go. Per x, we'll use github.com/mholt/caddy/caddy for the package import path. Use go get to fetch and install Caddy:. go get -u github.com/mholt/caddy/caddy The command will take some time to complete, but you won't see any progress while it fetches the package and installs it. No output actually indicates that the command executed successfully.
When the command completes, you'll find Caddy's source code available at $GOPATH/src/github.com/mholt/caddy. In addition, since Caddy has an executable, it was automatically created and stored in the $GOPATH/bin directory.
Verify this by using which to print the location of the executable:. which caddy You'll see the following output. Output /home/sammy/work/bin/caddy Note: The go get command installs packages from the default branch of a Git repository, which in many cases is the master, or in-development branch. Make sure to review the instructions, usually located in the repository's README file, before using go get.
You can use Git commands like git checkout to select a different branch on sources obtained using the go get command. Review the tutorial to learn more about how to switch branches. Let's look at the process of installing Go packages in more detail, starting with creating executables from packages we've already obtained.
Step 2 — Building an Executable The go get command downloaded the source and installed Caddy's executable for us in a single command. But you may want to rebuild the executable yourself, or build an executable from your own code. The go build command builds executables. Although we already installed Caddy, let's build it again manually so we can get comfortable with the process. Execute go build and specify the package import path:. go build github.com/mholt/caddy/caddy As before, no output indicates successful operation. The executable will be generated in your current directory, with the same name as the directory containing the package.
In this case, the executable will be named caddy. If you're located in the package directory, you can omit the path to the package and simply run go build. To specify a different name or location for the executable, use the -o flag. Let's build an executable called caddy-server and place it in a build directory within the current working directory:. go build -o build/caddy-server github.com/mholt/caddy/caddy This command creates the executable, and also creates the./build directory if it doesn't exist. Now let's look at installing executables.
Step 3 — Installing an Executable Building an executable creates the executable in the current directory or the directory of your choice. Installing an executable is the process of creating an executable and storing it in $GOPATH/bin. The go install command works just like go build, but go install takes care of placing the output file in the right place for you. To install an executable, use go install, followed by the package import path.
Once again, use Caddy to try this out:. go install github.com/mholt/caddy/caddy Just like with go build, you'll see no output if the command was successful. And like before, the executable is created with the same name as the directory containing the package. But this time, the executable is stored in $GOPATH/bin. If $GOPATH/bin is part of your $PATH environmental variable, the executable will be available from anywhere on your operating system.
You can verify its location using which command:. which caddy You'll see the following output. Output of which /home/sammy/work/bin/caddy Now that you understand how go get, go build, and go install work, and how they're related, let's explore one of the most popular Go features: creating executables for other target platforms.
Step 4 — Building Executables for Different Architectures The go build command lets you build an executable file for any Go-supported target platform, on your platform. This means you can test, release and distribute your application without building those executables on the target platforms you wish to use. Cross-compiling works by setting required environment variables that specify the target operating system and architecture.
We use the variable GOOS for the target operating system, and GOARCH for the target architecture. To build an executable, the command would take this form:.
env GOOS= target-OS GOARCH= target-architecture go build package-import-path The env command runs a program in a modified environment. This lets you use environment variables for the current command execution only. The variables are unset or reset after the command executes. Outputcaddy.exe Note: You can use the -o flag to rename the executable or place it in a different location. However, when building an executable for Windows and providing a different name, be sure to explicitly specify the.exesuffix when setting the executable's name. Let's look at scripting this process to make it easier to release software for multiple target environments. Step 5 — Creating a Script to Automate Cross-Compilation The process of creating executables for many platforms can be a little tedious, but we can create a script to make things easier.
The script will take the package import path as an argument, iterate through a predefined list of operating system and platform pairs, and generate an executable for each pair, placing the output in the current directory. Each executable will be named with the package name, followed by the target platform and architecture, in the form package-OS-architecture. This will be a universal script that you can use on any project. Switch to your home directory and create a new file called go-executable-build.bash in your text editor:. cd. nano go-executable-build.bash We will start our script with a shebang line. This line defines which interpreter will parse this script when it's run as an executable.
Add the following line to specify that bash should execute this script. If -z '$package' ; then echo 'usage: $0 ' exit 1 fi This if statement checks the value of the $package variable. If it's not set, we use echo to print the correct usage, and then terminate the script using exit. Exit takes a return value as an argument, which should be 0 for successful executions and any non-zero value for unsuccessful executions. We use 1 here since the script wasn't successful. Note: If you want to make this script work with a predefined package, change the package variable to point to that import path.
If $GOOS = 'windows' ; then outputname+='.exe' fi env GOOS=$GOOS GOARCH=$GOARCH go build -o $outputname $package done Finally, we should check to see if there were errors building the executable. For example, we might run into an error if we try to build a package we don't have sources for. We can check the go build command's return code for a non-zero value. The variable $? Contains the return code from a previous command's execution. If go build returns anything other than 0, there was a problem, and we'll want to exit the script.
Add this code to the for loop, after the go build command and above the done keyword. Example ls outputcaddy-darwin-amd64 caddy-windows-386.exe caddy-windows-amd64.exe To change the target platforms, just change the platforms variable in your script. Conclusion In this tutorial, you've learned how to use Go's tooling to obtain packages from version control systems, as well as build and cross-compile executables for different platforms. You also created a script that you can use to cross-compile a single package for many platforms. To make sure your application works correctly, you can take a look at and continuous integration like and for testing on Windows.
If you are interested in Caddy and how to use it, take a look at.
Install and Set Up kubectl Use the Kubernetes command-line tool, to deploy and manage applications on Kubernetes. Using kubectl, you can inspect cluster resources; create, delete, and update components; look at your new cluster; and bring up example apps. Before you begin You must use a kubectl version that is within one minor version difference of your cluster. For example, a v1.2 client should work with v1.1, v1.2, and v1.3 master. Using the latest version of kubectl helps avoid unforeseen issues.
Install kubectl Here are a few methods to install kubectl. Install kubectl binary using native package management. Cat /etc/yum.repos.d/kubernetes.repo kubernetes name=Kubernetes baseurl=enabled=1 gpgcheck=1 repogpgcheck=1 gpgkey=EOF yum install -y kubectl Install with snap on Ubuntu If you are on Ubuntu or one of other Linux distributions that support package manager, kubectl is available as a application. Switch to the snap user and run the installation command: sudo snap install kubectl -classic.
Test to ensure the version you installed is sufficiently up-to-date: kubectl version Install with Homebrew on macOS If you are on macOS and using package manager, you can install kubectl with Homebrew. Run the installation command: brew install kubernetes-cli. Test to ensure the version you installed is sufficiently up-to-date: kubectl version Install with Macports on macOS If you are on macOS and using package manager, you can install kubectl with Macports. Run the installation command: port install kubectl. Test to ensure the version you installed is sufficiently up-to-date: kubectl version Install with Powershell from PSGallery If you are on Windows and using package manager, you can install and update kubectl with Powershell. Run the installation commands (making sure to specify a DownloadLocation): Install-Script -Name install-kubectl -Scope CurrentUser -Force install-kubectl.ps1 -DownloadLocation.
Note: Updating the installation is performed by rerunning the two commands listed in step 1. Install with Chocolatey on Windows If you are on Windows and using package manager, you can install kubectl with Chocolatey.
Run the installation command: choco install kubernetes-cli. Test to ensure the version you installed is sufficiently up-to-date: kubectl version. Change to your%HOME% directory: For example: cd C: users yourusername. Create the.kube directory: mkdir.kube.
Change to the.kube directory you just created: cd.kube. Configure kubectl to use a remote Kubernetes cluster: New-Item config -type file. Download the latest release: curl -LO -s To download a specific version, replace the $(curl -s portion of the command with the specific version.
For example, to download version v1.12.0 on macOS, type: curl -LO. Make the kubectl binary executable.
Chmod +x./kubectl. Move the binary in to your PATH.
Go Cli Binary Not Being Generated For Macro
Sudo mv./kubectl /usr/local/bin/kubectl. Download the latest release with the command: curl -LO -s To download a specific version, replace the $(curl -s portion of the command with the specific version. For example, to download version v1.12.0 on Linux, type: curl -LO. Make the kubectl binary executable.
Chmod +x./kubectl. Move the binary in to your PATH. Sudo mv./kubectl /usr/local/bin/kubectl. Download the latest release v1.12.0 from.
Or if you have curl installed, use this command: curl -LO To find out the latest stable version (for example, for scripting), take a look at. Add the binary in to your PATH. Configure kubectl In order for kubectl to find and access a Kubernetes cluster, it needs a, which is created automatically when you create a cluster using kube-up.sh or successfully deploy a Minikube cluster. See the for more about creating clusters. If you need access to a cluster you didn’t create, see the.
Go Cli Binary Not Being Generated For Mac
By default, kubectl configuration is located at /.kube/config. Check the kubectl configuration Check that kubectl is properly configured by getting the cluster state.