{"id":22849988,"url":"https://github.com/binzume/gotosh","last_synced_at":"2026-04-17T01:31:59.425Z","repository":{"id":265195862,"uuid":"895415347","full_name":"binzume/gotosh","owner":"binzume","description":"PoC for generating shell script from subset of Go.","archived":false,"fork":false,"pushed_at":"2025-01-12T09:40:19.000Z","size":205,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-31T06:29:29.909Z","etag":null,"topics":["bash","go","shell-script"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/binzume.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-28T07:01:08.000Z","updated_at":"2025-03-23T04:11:52.000Z","dependencies_parsed_at":"2024-12-12T20:26:10.944Z","dependency_job_id":"ba3d7ab9-9294-4055-849c-d8c81cb423dd","html_url":"https://github.com/binzume/gotosh","commit_stats":null,"previous_names":["binzume/gotosh"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/binzume/gotosh","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binzume%2Fgotosh","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binzume%2Fgotosh/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binzume%2Fgotosh/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binzume%2Fgotosh/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/binzume","download_url":"https://codeload.github.com/binzume/gotosh/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binzume%2Fgotosh/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31911455,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-16T18:22:33.417Z","status":"ssl_error","status_checked_at":"2026-04-16T18:21:47.142Z","response_time":69,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","go","shell-script"],"created_at":"2024-12-13T05:06:05.222Z","updated_at":"2026-04-17T01:31:59.391Z","avatar_url":"https://github.com/binzume.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go to sh\n\nGo(のサブセット)で書かれたプログラムをシェルスクリプトに変換(トランスパイル)するプログラムです。\nGoで実装されています。\n\nシェルスクリプトを型付きの言語で書くために実装しました。\nGoでコンパイルしたバイナリを実行するのが困難な環境のために作ったので、可能な限りBusyBoxのash等でも動作するようにしています。\n\nSupported:\n\n- Types: `int`, `string`, `float32/64`, `[]int`, `[]string`, `struct`\n- Go keywords: func, if, else, for, break, continue, const, var, struct, append, len, go\n\nTODO:\n\n- jq, curl support\n- Convert compiler/compiler.go to goto.sh\n\n# Usage\n\nSee [examples](examples) folder\n\n```bash\ngo run . examples/fizz_buzz.go \u003e fizz_buzz.sh\nchmod a+x fizz_buzz.sh\n./fizz_buzz.sh\n```\n\n### Input(fizz_buzz.go)\n\n```go\npackage main\n\nimport \"fmt\"\n\nconst fizz = \"Fizz\"\nconst buzz = \"Buzz\"\n\nfunc FizzBuzz(n int) {\n\tfor i := 1; i \u003c= n; i++ {\n\t\tif i%15 == 0 {\n\t\t\tfmt.Println(fizz + buzz)\n\t\t} else if i%3 == 0 {\n\t\t\tfmt.Println(fizz)\n\t\t} else if i%5 == 0 {\n\t\t\tfmt.Println(buzz)\n\t\t} else {\n\t\t\tfmt.Println(i)\n\t\t}\n\t}\n}\n\nfunc main() {\n\tFizzBuzz(50)\n}\n```\n\n### Output(fizz_buzz.sh)\n\n```bash\n#!/bin/bash\n\nfizz=\"Fizz\"\nbuzz=\"Buzz\"\nFizzBuzz() {\n  local n=\"$1\"; shift\n  local i=1\n  while [ $(( i \u003c= n )) -ne 0 ]; do :\n    if [ $(( i%15 == 0 )) -ne 0 ]; then :\n      echo \"$fizz\"\"$buzz\"\n    elif [ $(( i%3 == 0 )) -ne 0 ]; then :\n      echo \"$fizz\"\n    elif [ $(( i%5 == 0 )) -ne 0 ]; then :\n      echo \"$buzz\"\n    else\n      echo $i\n    fi\n  : $(( i++ )); done\n}\n\nmain() {\n  FizzBuzz 50\n}\n\nmain \"${@}\"\n```\n\n## Supported functions\n\n- [shell.NArg](shell/builtin.go)\n- [shell.Arg](shell/builtin.go)\n- [shell.Exec](shell/builtin.go)\n- [shell.Do](shell/builtin.go)\n- [shell.SetFloatPrecision](shell/builtin.go)\n- [shell.ReadLine](shell/builtin.go)\n- [shell.Sleep](shell/builtin.go)\n- [shell.UnixTimeMs](shell/builtin.go)\n- [fmt.Print](https://pkg.go.dev/fmt#Print)\n- [fmt.Println](https://pkg.go.dev/fmt#Println)\n- [fmt.Printf](https://pkg.go.dev/fmt#Printf)\n- [fmt.Sprint](https://pkg.go.dev/fmt#Sprint)\n- [fmt.Sprintln](https://pkg.go.dev/fmt#Sprintln)\n- [fmt.Sprintf](https://pkg.go.dev/fmt#Sprintf)\n- [fmt.Fprint](https://pkg.go.dev/fmt#Fprint)\n- [fmt.Fprintln](https://pkg.go.dev/fmt#Fprintln)\n- [fmt.Fprintf](https://pkg.go.dev/fmt#Fprintf)\n- [strings.ReplaceAll](https://pkg.go.dev/strings#ReplaceAll)\n- [strings.ToUpper](https://pkg.go.dev/strings#ToUpper)\n- [strings.ToLower](https://pkg.go.dev/strings#ToLower)\n- [strings.TrimSpace](https://pkg.go.dev/strings#TrimSpace)\n- [strings.TrimPrefix](https://pkg.go.dev/strings#TrimPrefix)\n- [strings.TrimSuffix](https://pkg.go.dev/strings#TrimSuffix)\n- [strings.Split](https://pkg.go.dev/strings#Split)\n- [strings.Join](https://pkg.go.dev/strings#Join)\n- [strings.Contains](https://pkg.go.dev/strings#Contains)\n- [strings.IndexAny](https://pkg.go.dev/strings#IndexAny)\n- [strconv.Atoi](https://pkg.go.dev/strconv#Atoi)\n- [strconv.Itoa](https://pkg.go.dev/strconv#Itoa)\n- [os.Exit](https://pkg.go.dev/os#Exit)\n- [os.Chdir](https://pkg.go.dev/os#Chdir)\n- [os.Getwd](https://pkg.go.dev/os#Getwd)\n- [os.Getpid](https://pkg.go.dev/os#Getpid)\n- [os.Getppid](https://pkg.go.dev/os#Getppid)\n- [os.Getuid](https://pkg.go.dev/os#Getuid)\n- [os.Geteuid](https://pkg.go.dev/os#Geteuid)\n- [os.Getgid](https://pkg.go.dev/os#Getgid)\n- [os.Getegid](https://pkg.go.dev/os#Getegid)\n- [os.Hostname](https://pkg.go.dev/os#Hostname)\n- [os.Getenv](https://pkg.go.dev/os#Getenv)\n- [os.Setenv](https://pkg.go.dev/os#Setenv)\n- [os.Open](https://pkg.go.dev/os#Open)\n- [os.Create](https://pkg.go.dev/os#Create)\n- [os.Mkdir](https://pkg.go.dev/os#Mkdir)\n- [os.MkdirAll](https://pkg.go.dev/os#MkdirAll)\n- [os.Remove](https://pkg.go.dev/os#Remove)\n- [os.RemoveAll](https://pkg.go.dev/os#RemoveAll)\n- [os.Rename](https://pkg.go.dev/os#Rename)\n- [os.Pipe](https://pkg.go.dev/os#Pipe)\n- [math.Sqrt](https://pkg.go.dev/math#Sqrt)\n- [math.Pow](https://pkg.go.dev/math#Pow)\n- [math.Exp](https://pkg.go.dev/math#Exp)\n- [math.Log](https://pkg.go.dev/math#Log)\n- [math.Sin](https://pkg.go.dev/math#Sin)\n- [math.Cos](https://pkg.go.dev/math#Cos)\n- [math.Atan](https://pkg.go.dev/math#Atan)\n\nConstatns:\n\n- os.Stdin\n- os.Stdout\n- os.Stderr\n- runtime.Compiler // \"gotosh\" になっています\n- runtime.GOARCH\n- runtime.GOOS\n- shell.IsShellScript // トランスパイル後はtrueになるので、シェルスクリプト専用の処理への切り替えに使えます\n\n`GOTOSH_FUNC_` プレフィックスが付いた関数を定義することで、他のパッケージの関数を実装することができます。 (以下は `strings.Index()` を実装する例。暫定的な処置なので将来変わるかもしれません)\n\n```go\n// Implements strings.Index()\nfunc GOTOSH_FUNC_strings_Index(s, f string) int {\n\tfl := len(f)\n\tend := len(s) - fl + 1\n\tfor i := 0; i \u003c end; i++ {\n\t\tif s[i:i+fl] == f {\n\t\t\treturn i\n\t\t}\n\t}\n\treturn -1\n}\n\nfunc main() {\n\tfmt.Println(strings.Index(\"hello, world\", \"ld\")) // GOTOSH_strings_Index() will be invoked\n}\n```\n\n\n# 制限\n\nGoの文法をすべてサポートしているわけではありません。以下のキーワードは未サポートです。\n\n- map, interface, chan, make, new, defer, switch, case, select...\n\nまた、サポートされていても制限がある場合や挙動が異なる場合があります。\n\n## 型\n\n- 利用可能な型は、`int`, `string`, `float32/64` とそれらの struct や slice です\n- floatの演算を行うためには `bc` コマンドが必要です\n- ポインタは無いのですべての値渡しです\n\n### struct\n\nstructのサポートはまだ途中です。埋め込み等が無い単純なstructのみ対応しています。\n\n- sliceも入れることはできません\n- struct中にstructを直接定義できません\n- 初期化時にフィールド名を指定できません\n- structを返す関数を式の途中で使えません(一度変数に代入してください)\n\n```go\n// OK\ntype A struct {\n\ta int\n}\n\n// OK\ntype B struct {\n\ta A\n\tb1, b2 string\n}\n\n// Not supported\ntype C struct {\n\tc1 string\n\tc2 struct {\n\t\td int\n\t}\n}\n\nvar b1 = B{A{1}, \"abc\"} // OK\nvar b2 = B{b: \"abc\"} // Not supproted\n```\n\n### slice\n\nスライスの実装はbash専用です。zshの場合は `setopt KSH_ARRAYS` を追加する必要があると思います。\nまた、関数の最後の引数以外ではスライスを渡すことはできません。\n\n配列をサポートしていないシェルでも `shell.Args()` やスライスリテラル(`[]int{1,2,3,4}`等)を for range ループで処理することは可能です。\n\n## 関数\n\n### 引数\n\nsliceなども含めて全ての値は値渡しです。\n\n### 戻り値\n\n関数の結果は標準出力として返します。なので基本的に値を返す関数の内部で標準出力に何かを出力することはできません。\n標準出力以外で値を返すことを強制したい場合は以下の型(type alias)が使えます。(名前しか見てないので同名のtypeを定義しても動作します)\n\n- `shell.TempVarString` (= string) は _tmpN 変数を使って値を返します\n- `shell.StatusCode` (= byte) は関数の終了コードとして返します\n\n多値の戻り値をそのまま他の関数に渡すことはできません。例： `fmt.Println(functionReturnsMultiValues())`\n\n### レシーバ\n\nレシーバのある関数(メソッド)も使えますが、ポインタが無いのでメソッド内で自身の値を変更することはできません。\n\n## goroutine\n\nサブプロセスとして実行されます。無名関数はまだサポートされていないので通常の名前付きの関数を呼び出してください。\n\nまた、チャネルも使えないので、`os.Pipe()` (fifoが作られます)で作ったreader/writer等で通信してください。\n\n## 特殊な関数\n\nトランスパイラ自体を制御する関数です。トランスパイル時に処理されるので定数のみ渡せます。\n\n### shell.Do()\n\nトランスパイル時に渡された文字列をシェルスクリプトとして出力します。単一の文字列リテラルのみ利用できます。\n\n### shell.SetFloatPrecision()\n\nfloat型の精度を指定します。例えば、以下のプログラムをトランスパイルして実行すると円周率を1000桁出力します。\n\n```go\n\tshell.SetFloatPrecision(1000)\n\tfmt.Println(\"Pi:\", math.Atan(1)*4)\n```\n\n# Security\n\nashなどbash以外でも動作させるために実行時に `eval` している箇所が多くなっています。\nまた、あまりテストされていないのでスクリプト生成時のエスケープ漏れなどもありそうです。\n信頼されない入力値を受け取るプログラムには使わないか、入力を注意深くバリデートしてください。\n\n# License\n\nMIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinzume%2Fgotosh","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbinzume%2Fgotosh","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinzume%2Fgotosh/lists"}