A "Go Shell"

960 views
Skip to first unread message

Igor Almeida

unread,
Nov 15, 2009, 9:42:57 AM11/15/09
to golang-nuts
I was thinking of a project to take on so I could learn the language
and came up, in my delightful ignorance of the near future, with the
idea of implementing a POSIX shell. Its pun name would be "gosh" but
that might change due to the Go/Issue9 thing.
Has anyone given much thought to the subject? Do any of you have
interest in joining the effort?

TerryP

unread,
Nov 15, 2009, 12:00:34 PM11/15/09
to golang-nuts
I might be interested in it within the near future, after there is a
Go compiler & runtime available for my operating system ;).

--
TerryP
Just Another Computer Geek

Dwight Schauer

unread,
Nov 15, 2009, 12:13:04 PM11/15/09
to Igor Almeida, golang-nuts
I like the idea. Also, to further the discussion along these same
lines, I'd like to be able to put something #!/bin/gosh at the top of
any .go source file with a main function/package, so that one could
simple just edit/run. Compiling and linking would all be in maybe
using must be mounted tmpfs.

Since Go compiles and links so fast, for many go "scripts" the
compile/link/load time would not be noticeable enough to matter.

Who should care that it is not interpreted, it is just another form of
AOT/JIT and still very fast.

Adam Langley

unread,
Nov 15, 2009, 12:36:01 PM11/15/09
to Igor Almeida, golang-nuts
On Sun, Nov 15, 2009 at 6:42 AM, Igor Almeida <igor.c...@gmail.com> wrote:
> Has anyone given much thought to the subject? Do any of you have
> interest in joining the effort?

Sounds like it would be interesting. Not that you would probably run
into the current lack of signal handling ability pretty quickly.


AGL

TerryP

unread,
Nov 15, 2009, 12:43:56 PM11/15/09
to golang-nuts


On Nov 15, 5:13 pm, Dwight Schauer <dscha...@gmail.com> wrote:
> I like the idea. Also, to further the discussion along these same
> lines, I'd like to be able to put something #!/bin/gosh at the top of
> any .go source file with a main function/package, so that one could
> simple just edit/run. Compiling and linking would all be in maybe
> using must be mounted tmpfs.
>
> Since Go compiles and links so fast, for many go "scripts" the
> compile/link/load time would not be noticeable enough to matter.
>

That would not be a POSIX shell, Mr Schauer. What you want,
could probably be accomplished with a modified Go compiler, or
a rather sophisticated wrapper around one.

michael

unread,
Nov 15, 2009, 1:54:26 PM11/15/09
to golang-nuts
An interactive shell would be great. I love to try some code snippets
in the Python, Ruby or Erlang shell and I'm convinced that such a
thing would make Go even more attractive.

The day before yesterday I discovered Go. Something I was waiting for.

Good job you guys,
Michael

Adam Langley

unread,
Nov 15, 2009, 1:56:15 PM11/15/09
to michael, golang-nuts
On Sun, Nov 15, 2009 at 10:54 AM, michael <michael...@googlemail.com> wrote:
> An interactive shell would be great. I love to try some code snippets
> in the Python, Ruby  or Erlang shell and I'm convinced that such a
> thing would make Go even more attractive.

As TerryP points out, a Go REPL and a POSIX shell are very different.

Regarding a Go REPL, there's the beginnings of one in src/pkg/exp/eval.


AGL

Marcelo Castellani

unread,
Nov 15, 2009, 4:18:44 PM11/15/09
to Adam Langley, michael, golang-nuts
An Go REPL is excellent. I love tools like irb, for example.

jesse.dailey

unread,
Nov 15, 2009, 4:38:41 PM11/15/09
to golang-nuts

For those interested in how to use the eval package to create a simple
Go shell (not a POSIX shell, a mini-go cmd line like python has):

func shell(stdin io.Reader, stdout io.Writer, stderr io.Writer) (int)
{
r := bufio.NewReader(stdin);
o := bufio.NewWriter(stdout);
e := bufio.NewWriter(stderr);
world := eval.NewWorld();
prompt := func () {
o.WriteString(">>> ");
o.Flush();
};
readLine := func () string {
ret, err := r.ReadString('\n');
if err != nil {
os.Exit(1);
}
return ret;
};
execCmd := func (cmd string) int {
code, err := world.Compile(cmd); // parse and compile
if err != nil {
fmt.Fprint(e,err);
return 1;
}
typ := code.Type(); // check what type the code will return
if typ != nil {
fmt.Fprintf(o,"(%s) ",typ);
}
val, er := code.Run(); // run the code
if er != nil {
fmt.Fprint(e,err);
return 1;
}
if val != nil {
o.WriteString(val.String());
}
o.WriteString("\n");
return 0;
};

ret := 0;
cmd := "";
for {
prompt();
cmd = readLine();
ret = execCmd(cmd);
}
return ret;
}

It's limited currently to what the eval package can provide, which
precludes using imports (game breaker right now for it to be really
useful).


On Nov 15, 4:18 pm, Marcelo Castellani <marc...@hypequino.com> wrote:
> An Go REPL is excellent. I love tools like irb, for example.
>
> On 15/11/09 16:56, "Adam Langley" <a...@golang.org> wrote:
>
>
>
> > On Sun, Nov 15, 2009 at 10:54 AM, michael <michaelhusm...@googlemail.com>

Igor Almeida

unread,
Nov 15, 2009, 5:41:09 PM11/15/09
to golang-nuts
Okay, let's try to narrow things down a bit.

First and foremost, it is not my intention to write a Go REPL, as
there are at least two of those (Jesse's, below, and the one in
http://groups.google.com/group/golang-nuts/msg/3c23b5696b43c00f ), if
I understand their codes correctly.

What I intend to do is to read the sh(1P) manpage and implement
working "gosh" interactive shell. It sure looks quite an undertaking
and I'd be happy to receive any feedback on the idea.

I set up a repository at github, /igoralmeida/gosh, but there's
nothing there as of yet.

I'll post further details once I have them. Right now, I'm focusing on
the exec and os packages to have a VERY basic prototype running.

Igor Almeida

unread,
Nov 15, 2009, 9:02:51 PM11/15/09
to golang-nuts
FYI, the repository has finally been populated for the first time :)

On Nov 15, 7:41 pm, Igor Almeida <igor.cont...@gmail.com> wrote:
> Okay, let's try to narrow things down a bit.
>
> First and foremost, it is not my intention to write a Go REPL, as
> there are at least two of those (Jesse's, below, and the one inhttp://groups.google.com/group/golang-nuts/msg/3c23b5696b43c00f), if

Adam Sampson

unread,
Nov 16, 2009, 7:17:04 AM11/16/09
to golang-nuts
"jesse.dailey" <jesse....@gmail.com> writes:

> func shell(stdin io.Reader, stdout io.Writer, stderr io.Writer) (int)
[...]
> readLine := func () string {

It might be nice to have a little bit of syntactic sugar so that those
two definitions could be written using the same syntax -- nested
functions like this are really useful.

--
Adam Sampson <a...@offog.org> <http://offog.org/>
Reply all
Reply to author
Forward
0 new messages