For me, it took a while to twig that the normal unit of compilation is the 
package, *and* that a package is normally a *directory* of files.  "go run 
main.go" breaks this by having a special case where the package is a single 
file (or worse, a collection of listed files).

I suggest that newcomers are told to create an empty directory, create 
main.go in that directory, and then use "go run ." (a literal dot), "go 
build .", "go fmt ." etc.  Of course, this means that if you have several 
programs you need to create separate directories for them - but that's the 
right thing to do.  Plus, these days, you're soon going to need "go mod 
init myprog" anyway, which will create a go.mod alongside it.

When starting I had a single directory with separate programs "prog1.go", 
"prog2.go", "prog3.go" and then did "go run prog1.go" or whatever - which 
kind-of works, but breaks in all kinds of unexpected ways later.
 
On Thursday, 25 February 2021 at 08:09:31 UTC Paul Foerster wrote:

> Hi Volker,
>
> > On 25. Feb, 2021, at 08:46, Volker Dobler <dr.volke...@gmail.com> wrote:
> > 
> > I think there is a major problem with "go run main.go": It creates
> > a _false mental model_ of how Go code is built and executed in the
> > minds of _beginners_.
>
> I am a Go newbie and I agree, though I immediately understood that "go run 
> ..." should only be for the developing/testing phase. But then, I did a 
> little C before and know about what compilers do. But I also found tons of 
> stuff immediately that said to "go build ...".
>
> Also, experience is good, but RTFM is also good, especially when starting 
> with something completely new.
>
> Maybe rename the "run" command to "test", so it would be "go test ..." 
> instead of "go run ..."?
>
> Just a thought.
>
> Cheers,
> Paul
>
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/632ca1a9-36d8-4143-abd8-ae79ad7762e2n%40googlegroups.com.

Reply via email to