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=${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'
89 golang_install_path=$HOME/.local/opt/${GOLANG_VER}
90 mkdir -p $golang_install_path
92 # TODO warn if existing golang in path my take precedence
93 if [ -e "$golang_install_path/bin/go" ]; then
94 # golang of some version is already installed
95 #echo "${GOLANG_VER}" == "$($golang_install_path/bin/go version | cut -d ' ' -f 3 2>/dev/null)"
96 if [ "${GOLANG_VER}" == "$($golang_install_path/bin/go version | cut -d ' ' -f 3 2>/dev/null)" ]; then
97 echo ${GOLANG_VER} already installed at $golang_install_path
102 GOLANG_PRE="${GOLANG_VER}.${GOLANG_OS}-${GOLANG_ARCH}"
103 GOLANG_REMOTE="https://dl.google.com/go/${GOLANG_PRE}.tar.gz"
104 GOLANG_LOCAL="$my_tmp/${GOLANG_PRE}.tar.gz"
105 GOLANG_UNTAR="$my_tmp/${GOLANG_PRE}"
107 if [ -n "$(command -v curl 2>/dev/null | grep curl)" ]; then
108 curl -fSL ${GOLANG_REMOTE} -o ${GOLANG_LOCAL} || echo 'error downloading golang'
109 elif [ -n "$(command -v wget 2>/dev/null | grep wget)" ]; then
110 wget ${GOLANG_REMOTE} -O ${GOLANG_LOCAL} || echo 'error downloading golang'
112 echo "'wget' and 'curl' are missing. Please run the following command and try again"
113 echo " sudo apt-get install --yes curl wget"
117 mkdir -p ${GOLANG_UNTAR}/
118 # --strip-components isn't portable, switch to portable version by performing move step after untar
119 tar xf ${GOLANG_LOCAL} -C ${GOLANG_UNTAR}/ #--strip-components=1
120 mv ${GOLANG_UNTAR}/go/* ${GOLANG_UNTAR}/
121 rm -rf ${GOLANG_UNTAR}/go # clean up the temporary unzip folder
122 if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
123 echo $sudo_cmd rsync -Krl "${GOLANG_UNTAR}/" "$golang_install_path/"
124 rsync -Krl "${GOLANG_UNTAR}/" "$golang_install_path/" 2>/dev/null || $sudo_cmd rsync -Krl "${GOLANG_UNTAR}/" "$golang_install_path/"
126 echo $sudo_cmd cp -Hr "${GOLANG_UNTAR}/*" "$golang_install_path/"
127 cp -Hr "${GOLANG_UNTAR}"/* "$golang_install_path/" 2>/dev/null || $sudo_cmd cp -Hr "${GOLANG_UNTAR}"/* "$golang_install_path/"
129 rm -rf "${GOLANG_UNTAR}"
135 # TODO add more than one at a time
136 pathman add $golang_install_path/bin
137 mkdir -p $HOME/go/bin
138 pathman add $HOME/go/bin
139 echo "go get golang.org/x/tools/cmd/goimports"
140 $golang_install_path/bin/go get golang.org/x/tools/cmd/goimports > /dev/null 2>/dev/null