Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import local Go package in GAE

How to import local packages in Golang + GAE?

I wanna something like this:

app/
-app.yaml
-/my_app
--my_app.go
--/package1
---package1.go

Listing of my_app.go:

package my_app

import (
  "http"
  "./package1"
)

func init() {
  http.HandleFunc("/", package1.index)
}

Listing of package1.go:

package package1

import (
  "http"
  "fmt"
)

func index (w http.ResponseWriter, r * http.Request) {
  fmt.Fprint(w, "I'm index page =) ")
}

I this case I have an error like:

/path/to/project/my_app/my_app.go:5: can't find import: ./package1
2011/11/03 10:50:51 go-app-builder: Failed building app: failed running 6g: exit status 1

Thanks for help.

like image 936
Rusfearuth Avatar asked Nov 03 '11 03:11

Rusfearuth


People also ask

How do I add local packages to go?

Check your GOPATH in environment variables and set it to the directory which contains all Go files. Create a new folder with the name of the package you wish to create. In the folder created in step 2, create your go file that holds the Go package code you wish to create.

How do I import a go package?

To import a package, we use import syntax followed by the package name. 💡 Unlike other programming languages, a package name can also be a subpath like some-dir/greet and Go will automatically resolve the path to the greet package for us as you will see in the nested package topic ahead.

How do I use local library in Golang?

Golang Import Local PackagesIn the root of your project, aka, the workspace directory, create a main.go file. In the example above, we add 3 import clauses to import all the packages in our program. Once imported, we can use the exported code in the packages.


2 Answers

As noted in the comments to dupoxy's answer, the way to import a local package in the given situation is to import as "my_app/package1":

import (
    "http"
    "my_app/package1"
)
like image 102
Darshan Rivka Whittle Avatar answered Sep 28 '22 03:09

Darshan Rivka Whittle


You either need to link or copy the packages to your application directory. The path relative to the root of the application directory should match the import path. To use package1, you should configure your app directory to look like this:

app.yaml
yourapp/yourapp.go
package1/package1.go

from https://groups.google.com/d/msg/golang-nuts/coEvrWIJGTs/75GzcefKVcIJ

like image 33
dupoxy Avatar answered Sep 28 '22 02:09

dupoxy