Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Simple example #56

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

coolaj86
Copy link

Re: #55

I've been banging my head trying to figure out how to get a simple example to work to no avail. This is my attempt so far.

This is what I expect to be able to do:

git clone https://github.com/shurcooL/vfsgen
cd vfsgen/
cd example/
go generate
go build
./example

Right now I get this error at the go generate step:

2018/09/21 23:58:18 can't import package "example.com/foo/example": cannot find package "example.com/foo/example" in any of:
	/usr/local/go/src/example.com/foo/example (from $GOROOT)
	/Users/aj/go/src/example.com/foo/example (from $GOPATH)
exit status 1
main.go:1: running "go": exit status 1

The error seems to be coming from vfsgendev, but I have no idea why it would be looking in GOPATH. It's as if it's using an older version of go. go.mod is in the directory with the correct name and go build runs fine.

Any ideas?

@dmitshur
Copy link
Member

dmitshur commented Sep 24, 2018

I'll treat this PR as a question. I don't think we should merge an example into the codebase here, but we should describe one in the README (perhaps it can live in a separate repository, etc.).

You're definitely on the right track, so let me guide you further.

You'll want to create 2 Go packages to make your example work with vfsgendev. We will call the first package assets, and it will be a library responsible for exposing the assets. The second package will be a Go command, meaning it'll have package main and compile into a binary you can run. It will import the assets library.

Inside assets, you'll want to create an exported variable, for example Assets:

package assets

var Assets http.FileSystem = http.Dir("./assets")

To make this play well with vfsgendev, you'll also add a build constraint:

// +build dev

package assets

var Assets http.FileSystem = http.Dir("./assets")

So now when the assets package is build with dev build tag, it will read from the physical "./assets" directory via http.Dir.

You need to be mindful of what import path you use for the assets package. For example, it could be github.com/shurcooL/vfsgen/example/assets. Then, your vfsgendev invocation should be:

//go:generate go run github.com/shurcooL/vfsgen/cmd/vfsgendev -source="github.com/shurcooL/vfsgen/example/assets".Assets

You were getting the error:

2018/09/21 23:58:18 can't import package "example.com/foo/example": cannot find package "example.com/foo/example" in any of:
	/usr/local/go/src/example.com/foo/example (from $GOROOT)
	/Users/aj/go/src/example.com/foo/example (from $GOPATH)

It's because the example.com/foo/example package didn't exist in your GOPATH, so it couldn't be found. You could create it, or you could use the github.com/shurcooL/vfsgen/example/assets import path I mentioned above.

Let me know if this helps you get further, or if something else is unclear.

This is documented, although not in the best way, at https://github.com/shurcooL/vfsgen#go-generate-usage and https://github.com/shurcooL/vfsgen#vfsgendev-usage.

I think you might also be running into the issue that modules aren't yet supported by vfsgendev; it's only looking for packages in your GOPATH workspace. See #50.

@dmitshur
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants