-2

I know this question has been asked before and I've tried all possible answers, but nothing still didn't help me.

However to refresh the question again and to elaborate my problem. I'm actually trying to include a simple file to the main.go file. My folder structure and the rest of the information as follows:

\src\
   Multi-file\
     lib\Car.go
   main.go

Car.go

 package main

    type Car struct {
        numberOfDoors int
        cylinders int
    }

main.go

package main 

import (
    "fmt"
)

func main() {
    c := Car{4,6}
    fmt.Println(c)
}

When I complie the main.go I'm getting the following error

# command-line-arguments
.\main.go:8: undefined: Car

Here is my go env details:

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\xampp\htdocs\golang
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GO15VENDOREXPERIMENT=
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1

FYI: I tried including as package also that didn't even help me out.

Any help would be extremely appreciated.

Community
  • 1
  • 1
FR STAR
  • 662
  • 4
  • 24
  • 50
  • 3
    If you were a Go linker, how would you decide were to find Car definition in your \src structure? Be more friendly to machine and structure your packages as suggested in manuals and import explicitly what you need – Uvelichitel Jan 09 '16 at 16:43

2 Answers2

5

Set up your workspace for programs and libraries by following the instructions: How to Write Go Code.

For your example, a two file main package,

src/car
├── car.go
└── main.go

$ cd src/car
$ go run main.go car.go
{4 6}
$ go build car && ./car
{4 6}
$ go install car && car
{4 6}
$ 

main.go:

package main

import (
    "fmt"
)

func main() {
    c := Car{4, 6}
    fmt.Println(c)
}

car.go:

package main

type Car struct {
    numberOfDoors int
    cylinders     int
}
peterSO
  • 158,998
  • 31
  • 281
  • 276
  • Wow, you really cracked the code, however I would like to clarify few things, 1). go run *.go (this line of code didn't work for me) 2) and 3) did worked, after this line of codes don't I need to execute the following **go run main.go**? as @arainone explained below. – FR STAR Jan 09 '16 at 17:55
  • @FRSTAR: Wildcard globs, for example, `*.go`, are a Linux shortcut. On Windows, list each file, for example, `go run main.go car.go`. – peterSO Jan 09 '16 at 18:02
  • 1
    @FRSTAR: `go run` compiles and runs the main package comprising the named Go source files. `go build` compiles the packages named by the import paths, along with their dependencies, but it does not install the results. `go install` compiles and installs the packages named by the import paths, along with their dependencies. See [Command go](https://golang.org/cmd/go/). – peterSO Jan 09 '16 at 18:07
1

You should not have 2 files, that are declared in the same package but are not residing in the same directory. One solution could be to have this directory structure instead:

src\
  car\
    car.go
  main.go

car.go

package car

type Car struct {
    NumberOfDoors int
    Cylinders     int
}

main.go

package main

import (
    "car"
    "fmt"
)

func main() {
    c := car.Car{4, 6}
    fmt.Println(c)
}

Of course, Car structure not being in main package, I had to:

  • change to uppercase the first letter of NumberOfDoors and Cylinders fields from car.Car struct, so that they can be accessed from main package
  • replace the call to Car{4,6} with car.Car{4,6}
  • code in car.go is not part of package main anymore, you can view it as a car library now. That's why the Car struct is now declared being part of package car

Like that, you can build and run in one step with:

go run main.go
arainone
  • 1,928
  • 17
  • 28
  • I tried your code and I'm getting following error, **main.go:4:5: import "car" is a program, not an importable package** @peterSO code did worked, but I'm curious with your answer, so could u pls let me know whats the issue? – FR STAR Jan 09 '16 at 17:48
  • @FRSTAR i don't have this error, the code is working for me. Are you sure you replaced `package main with `package car` in `car.go` file? I forgot to indicate this point in the explanations of my code, i'm adding it now – arainone Jan 09 '16 at 20:49