Bizarre Error Message (Go 1.9.2)

233 views
Skip to first unread message

Jon Forrest

unread,
Nov 2, 2017, 4:45:20 PM11/2/17
to golang-nuts

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.

Cordially,
Jon Forrest


Ian Lance Taylor

unread,
Nov 2, 2017, 4:56:10 PM11/2/17
to Jon Forrest, golang-nuts
`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

Raffaele Sena

unread,
Nov 2, 2017, 5:27:47 PM11/2/17
to Ian Lance Taylor, Jon Forrest, golang-nuts
Or you use the "standard" unix trick:

go run ls.go -- ls.go



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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Karan Chaudhary

unread,
Nov 6, 2017, 5:13:10 AM11/6/17
to golang-nuts
`go run` takes multiple files as arguments. 

Interesting.  Didn't know this was possible.  Until now i used to fallback to using go build if code was distributed over multiple files. (When i'm testing some case which includes few files) 
Reply all
Reply to author
Forward
0 new messages