Or you use the "standard" unix trick:

go run ls.go -- ls.go


On Thu, Nov 2, 2017 at 1:55 PM, Ian Lance Taylor <i...@golang.org> wrote:

> On Thu, Nov 2, 2017 at 10:37 AM, Jon Forrest <nob...@gmail.com> wrote:
> >
> > I'm learning Go. Whenever I learn a new programming language I like to
> try
> > to recreate the Unix 'ls' command
> > in that language. I've found that such an exercise is often a good way to
> > get familiar with what a language offers.
> >
> > Here's my environment:
> >
> > $ go version
> > go version go1.9.2 linux/amd64
> > $ cat /etc/redhat-release
> > CentOS Linux release 7.4.1708 (Core)
> >
> > Consider the following trivial program which I've saved as "ls.go":
> >
> > package main
> >
> > import (
> >     "fmt"
> >     "os"
> > )
> >
> > func main() {
> >     for _, arg := range os.Args[1:] {
> >         fi, err := os.Stat(arg)
> >         if err != nil {
> >             fmt.Println(err)
> >             return
> >         }
> >         switch mode := fi.Mode(); {
> >         case mode.IsRegular():
> >             fmt.Println("regular file")
> >         case mode.IsDir():
> >             fmt.Println("directory")
> >         }
> >     }
> > }
> >
> >
> > When I run 'go build ls.go' and then './ls ls.go' I see the output I
> expect,
> > which is
> > 'regular file'.
> >
> > However, when I run 'go run ls.go ls.go' I get the bizarre message
> >
> > can't load package: package main: case-insensitive file name collision:
> > "ls.go" and "ls.go"
> >
> > This is on an xfs filesystem.
> >
> > I don't understand why this error message appeared, nor what it's trying
> to
> > tell me.
> > Plus, I don't understand why running the result of 'go build' works, but
> > building
> > and running the program using 'go run' fails.
>
> `go run` takes multiple files as arguments.  When you type `go run
> ls.go ls.go` you are asking the go tool to build a Go program that
> consists of the contents of ls.go repeated twice.  Before it even gets
> to that point, it says "wait, you've told me the same file name twice,
> that can't be right" and then it starts talking about a
> case-insensitive file name which is certainly confusing.  This is kind
> of hard to avoid using `go run`, as it takes any argument that ends
> with ".go" as meaning a Go file to compile.
>
> Ian
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to