From 0f6f356b8505effd2161037b79cc4f2c7a925d20 Mon Sep 17 00:00:00 2001 From: Austin Cawley-Edwards Date: Tue, 9 May 2023 10:40:46 -0400 Subject: [PATCH] Add dynamic GOROOT setting for zsh (#56) Co-authored-by: Kenny Parnell --- README.md | 7 +++++++ set-env.zsh | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 set-env.zsh diff --git a/README.md b/README.md index 4527044..b020900 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,13 @@ asdf plugin-add golang https://github.com/kennyp/asdf-golang.git Check the [asdf](https://github.com/asdf-vm/asdf) readme for instructions on how to install & manage versions of go. +## `GOROOT` + +To set `GOROOT` in your shell's initialization add the following: + +**zsh shell** +`. ~/.asdf/plugins/golang/set-env.zsh` + ## When using `go get` or `go install` After using `go get` or `go install` to install a package you need to run `asdf reshim golang` to get any new shims. diff --git a/set-env.zsh b/set-env.zsh new file mode 100644 index 0000000..d0a38b4 --- /dev/null +++ b/set-env.zsh @@ -0,0 +1,17 @@ +function absolute_dir_path { + local absolute_path + absolute_path="$( cd -P "$( dirname "$1" )" && pwd )" + echo "$absolute_path" +} + +asdf_update_golang_env() { + local go_path + go_path="$(asdf which go)" + if [[ -n "${go_path}" ]]; then + export GOROOT + GOROOT="$(dirname "$(absolute_dir_path "${go_path}")")" + fi +} + +autoload -U add-zsh-hook +add-zsh-hook precmd asdf_update_golang_env