4 # homepage: https://golang.org
5 # tagline: The Go Programming Language tools
7 # Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
16 # cat << EOF >> main.go
24 # fmt.Println("Hello, World!")
40 # TODO handle v1.4 / go1.4 / lack of go1.4.0
41 GOLANG_VER=${X_WEBI_VERSION:-}
42 GOLANG_VER="${GOLANG_VER:-go}" # Search for 'go' at the least
44 # WEBI_ARCH uses only slightly different names from GOLANG_ARCH
45 GOLANG_OS="${WEBI_OS}" # linux or darwin
46 GOLANG_ARCH="${WEBI_ARCH}"
47 if [ "x86" == "$GOLANG_ARCH" ]; then
58 get_golang_version() {
59 # sort -rV # will sort by version number, but it appears these are already sorted
60 # cut -f 1 # gets only the first column
61 # head -n 1 # gets only the most recent version
62 # <td class="filename"><a class="download" href="https://dl.google.com/go/go1.13.4.darwin-amd64.tar.gz">go1.13.4.darwin-amd64.tar.gz</a></td>
64 my_count=$(awk -F"${my_char}" '{print NF-1}' <<< "${GOLANG_VER}")
65 # get the latest version if partial
66 if [ $my_count -ne 2 ]; then
67 if [ "$GOLANG_VER" != "go" ]; then
68 GOLANG_VER="$GOLANG_VER\\."
71 if [ -n "$(type -p curl)" ]; then
73 elif [ -n "$(type -p wget)" ]; then
74 get_http="wget --quiet -O -"
76 echo "Found neither 'curl' nor 'wget'. Can't Continue."
79 GOLANG_VER=$($get_http "https://golang.org/dl/" | grep filename.*download | grep ${GOLANG_VER} | grep ${GOLANG_ARCH} | grep ${GOLANG_OS} | cut -d '"' -f 6 | cut -d '/' -f5 | cut -d '.' -f 1-3 | sed 's/\.\(freebsd\|darwin\|linux\|windows\|src\).*//' | head -n 1) \
80 || echo 'error automatically determining current Golang version'
84 echo -n "Checking for latest golang version... "
91 golang_install_path=$HOME/.local/opt/${GOLANG_VER}
92 mkdir -p $golang_install_path
94 # TODO warn if existing golang in path my take precedence
95 if [ -e "$golang_install_path/bin/go" ]; then
96 # golang of some version is already installed
97 #echo "${GOLANG_VER}" == "$($golang_install_path/bin/go version | cut -d ' ' -f 3 2>/dev/null)"
98 if [ "${GOLANG_VER}" == "$($golang_install_path/bin/go version | cut -d ' ' -f 3 2>/dev/null)" ]; then
99 echo ${GOLANG_VER} already installed at $golang_install_path
104 GOLANG_PRE="${GOLANG_VER}.${GOLANG_OS}-${GOLANG_ARCH}"
105 GOLANG_REMOTE="https://dl.google.com/go/${GOLANG_PRE}.tar.gz"
106 GOLANG_LOCAL="$my_tmp/${GOLANG_PRE}.tar.gz"
107 GOLANG_UNTAR="$my_tmp/${GOLANG_PRE}"
109 echo Downloading $GOLANG_REMOTE
110 if [ -n "$(command -v curl 2>/dev/null | grep curl)" ]; then
111 curl -fSL ${GOLANG_REMOTE} -o ${GOLANG_LOCAL} || echo 'error downloading golang'
112 elif [ -n "$(command -v wget 2>/dev/null | grep wget)" ]; then
113 wget ${GOLANG_REMOTE} -O ${GOLANG_LOCAL} || echo 'error downloading golang'
115 echo "'wget' and 'curl' are missing. Please run the following command and try again"
116 echo " sudo apt-get install --yes curl wget"
120 echo Installing $GOLANG_LOCAL
121 mkdir -p ${GOLANG_UNTAR}/
122 # --strip-components isn't portable, switch to portable version by performing move step after untar
123 tar xf ${GOLANG_LOCAL} -C ${GOLANG_UNTAR}/ #--strip-components=1
124 mv ${GOLANG_UNTAR}/go/* ${GOLANG_UNTAR}/
125 rm -rf ${GOLANG_UNTAR}/go # clean up the temporary unzip folder
126 if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
127 echo $sudo_cmd rsync -Krl "${GOLANG_UNTAR}/" "$golang_install_path/"
128 rsync -Krl "${GOLANG_UNTAR}/" "$golang_install_path/" 2>/dev/null || $sudo_cmd rsync -Krl "${GOLANG_UNTAR}/" "$golang_install_path/"
130 echo $sudo_cmd cp -Hr "${GOLANG_UNTAR}/*" "$golang_install_path/"
131 cp -Hr "${GOLANG_UNTAR}"/* "$golang_install_path/" 2>/dev/null || $sudo_cmd cp -Hr "${GOLANG_UNTAR}"/* "$golang_install_path/"
133 rm -rf "${GOLANG_UNTAR}"
139 # TODO add more than one at a time
140 pathman add $golang_install_path/bin
141 mkdir -p $HOME/go/bin
142 pathman add $HOME/go/bin
143 echo "go get golang.org/x/tools/cmd/goimports"
144 $golang_install_path/bin/go get golang.org/x/tools/cmd/goimports > /dev/null 2>/dev/null