Blankman
Blankman

Reputation: 266910

My main.go file cannot see other files

I need some help understanding what is wrong with my file layout in a simple web application.

$GOPATH/src/example.com/myweb

I then have 2 files:

$GOPATH/src/example.com/myweb/main.go
$GOPATH/src/example.com/myweb/api.go

Both files have:

package main

The api.go file looks like:

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
    "time"
)

type API struct {
    URI   string
    Token  string
    Secret string
    client *http.Client
}

...

My main.go file looks like:

package main

import (
    "github.com/gorilla/mux"
    "html/template"
    "net/http"
)

var (
    templates = template.Must(template.ParseFiles("views/home.html", "views/history.html", "views/incident.html"))
    api = API{
        URI: "http://localhost:3000",
        Token: "abc",
        Secret: "123",
    }
)

func renderTemplate(w http.ResponseWriter, tmpl string, hp *HomePage) {
..
..
}

func WelcomeHandler(w http.ResponseWriter, r *http.Request) {
..
..
}

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/", WelcomeHandler)
    r.PathPrefix("/assets/").Handler(
        http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/"))))

    http.ListenAndServe(":9000", r)
}

In the code I excluded, I basically use structs that are defined in my api.go file, and I get this error when doing:

go run main.go
# command-line-arguments
./main.go:16: undefined: API
./main.go:23: undefined: User

What exactly am I doing wrong here?

I tried changing the package name in api.go to myweb but that didn't help.

Am I suppose to use the package name myweb? Is just 1 file suppose to have main?

Upvotes: 3

Views: 3621

Answers (2)

Apin
Apin

Reputation: 2668

You are using golang workspace project, which is good for the structure for your application and it also standardize.

When we use the golang workspace, you can not run single go file. You need to call go build / go install.

Install

go install example.com/myweb

The command above will compile your main package on example.com/myweb. And the myweb executable binary will be placed on the GOPATH/bin. And you can run it manually.

Build

go build example.com/myweb

The command is similar to go install but the binary executable file will be placed on the current directory when you call the command, instead of on GOPATH/bin (unless your current directory is GOPATH/bin).

For more information please check this link.

Upvotes: 1

Pedro Werneck
Pedro Werneck

Reputation: 41878

You're compiling only the main.go file. You should use:

go run main.go api.go

Or:

go run *.go

If you're writing a complex application, you might add everything to packages in subdirectories and have a single main.go file. For instance, etcd has an etcdmain subdirectory/package along with other subdirectories/packages. Something like:

/alarm    
/auth
/cmd
/etcdmain
...

And the main.go file is simply:

package main

import "github.com/coreos/etcd/etcdmain"

func main() {
    etcdmain.Main()
}

Upvotes: 4

Related Questions