Last active
July 11, 2022 17:41
-
-
Save rsperl/5d339968551ab0e7597c6fb024b48a01 to your computer and use it in GitHub Desktop.
create a local go playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# default root directory for goplay | |
GOPLAY_ROOT=""$HOME/goplay"" | |
# fn:goplay:create a go playground | |
function goplay() { | |
local module_name="${1:-anon}" | |
local ts="$(date +%Y-%m-%d_%H%M%S)" | |
local dirname="$GOPLAY_ROOT/$module_name-$ts" | |
local go_version=$(go version | awk '{print $3}' | sed -e 's/go//' | awk -F. '{print $1"."$2}') | |
mkdir -p "$dirname" | |
echo "creating $dirname/main.go" | |
cat <<EOF >"$dirname/main.go" | |
package main | |
import ( | |
"log" | |
"os" | |
) | |
func main() { | |
log.Println("your code here...") | |
os.Exit(0) | |
} | |
EOF | |
echo "creating $dirname/go.mod" | |
cat <<EOF >"$dirname/go.mod" | |
module $module_name | |
go $go_version | |
EOF | |
cd "$dirname" | |
echo "$dirname" | |
ls -l | |
} | |
goplay $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment