Skip to content

Instantly share code, notes, and snippets.

@TrQ-Hoan
Last active July 9, 2024 17:32
Show Gist options
  • Save TrQ-Hoan/af0b342160efb4da16506cadb5ea8358 to your computer and use it in GitHub Desktop.
Save TrQ-Hoan/af0b342160efb4da16506cadb5ea8358 to your computer and use it in GitHub Desktop.
Golang install on WSL
#!/bin/bash
package=$1
if [[ -z "$package" ]]; then
echo "usage: $0 <package-name>"
exit 1
fi
package_split=(${package//\// })
package_name=${package_split[-1]}
platforms=("windows/amd64" "windows/386" "linux/386" "linux/amd64")
for platform in "${platforms[@]}"
do
platform_split=(${platform//\// })
GOOS=${platform_split[0]}
GOARCH=${platform_split[1]}
output_name=$package_name'-'$GOOS'-'$GOARCH
if [ $GOOS = "windows" ]; then
output_name+='.exe'
fi
tput setaf 3; echo -n "--- $GOOS/$GOARCH ---"
tput sgr0; echo
env GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-s -w" -o $output_name $package
if [ $? -ne 0 ]; then
tput setaf 1; echo -n '[-] An error has occurred! Aborting the script execution...'
tput sgr0; echo
else
tput setaf 2; echo -n '[+] Done'
tput sgr0; echo
fi
done
go mod init <module name>
# build linux amd64 + stripped
gox -osarch="linux/amd64" -ldflags "-s -w"
# build linux amd64 + stripped + static link
gox -osarch="linux/amd64" -ldflags "-s -w -linkmode 'external' -extldflags '-static'"
# build windows x86 + stripped
gox -osarch="windows/386" -ldflags "-s"
env GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o <fileout.exe> <source.go>
env GOOS=windows GOARCH=386 go build -ldflags="-s -w" -o <fileout.exe> <source.go>
curl https://dl.google.com/go/go1.18.10.linux-amd64.tar.gz -O
tar -C /usr/local -xzf go1.18.10.linux-amd64.tar.gz
ln -s /usr/local/go/bin/go /usr/bin/go
ln -s /usr/local/go/bin/gofmt /usr/bin/gofmt
export GOPATH=/usr/local/go
go install github.com/mitchellh/gox@latest
ln -s /usr/local/go/bin/gox /usr/bin/gox
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment