urlshort
urlshort copied to clipboard
cannot find package "github.com/gophercises/urlshort"
I am using Windows 10, with WSL, and ZSH.
When I run go env
in my VSCode terminal I get the following:
GOOS="linux"
GOPATH="/home/maylortaylor"
GORACE=""
GOROOT="/usr/lib/go-1.10"
When i run VSCode task Go: Current GOPATH
, i get the following:
C:\gocode\
According to my Windows Environment Variables I have
GOPATH = C:\gocode\
and
my PATH variable has C:\gocode\bin\
added at the end.
App Structure
- urlshort
- main
- main.go
- main
- handler.go
Main.go
package main
import (
"fmt"
"net/http"
"github.com/gophercises/urlshort"
)
func main() {
mux := defaultMux()
// Build the MapHandler using the mux as the fallback
pathsToUrls := map[string]string{
"/urlshort-godoc": "https://godoc.org/github.com/gophercises/urlshort",
"/yaml-godoc": "https://godoc.org/gopkg.in/yaml.v2",
}
mapHandler := urlshort.MapHandler(pathsToUrls, mux)
// Build the YAMLHandler using the mapHandler as the
// fallback
yaml := `
- path: /urlshort
url: https://github.com/gophercises/urlshort
- path: /urlshort-final
url: https://github.com/gophercises/urlshort/tree/solution
`
yamlHandler, err := urlshort.YAMLHandler([]byte(yaml), mapHandler)
if err != nil {
panic(err)
}
fmt.Println("Starting the server on :8080")
http.ListenAndServe(":8080", yamlHandler)
}
func defaultMux() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/", hello)
return mux
}
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, world!")
}
Handler.go
package urlshort
import (
"net/http"
yaml "gopkg.in/yaml.v2"
)
func MapHandler(pathsToUrls map[string]string, fallback http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
if dest, ok := pathsToUrls[path]; ok {
http.Redirect(w, r, dest, http.StatusFound)
return
}
fallback.ServeHTTP(w, r)
}
}
func YAMLHandler(yamlBytes []byte, fallback http.Handler) (http.HandlerFunc, error) {
pathUrls, err := parseYaml(yamlBytes)
if err != nil {
return nil, err
}
pathsToUrls := buildMap(pathUrls)
return MapHandler(pathsToUrls, fallback), nil
}
func buildMap(pathUrls []pathUrl) map[string]string {
pathsToUrls := make(map[string]string)
for _, pu := range pathUrls {
pathsToUrls[pu.Path] = pu.URL
}
return pathsToUrls
}
func parseYaml(data []byte) ([]pathUrl, error) {
var pathUrls []pathUrl
err := yaml.Unmarshal(data, &pathUrls)
if err != nil {
return nil, err
}
return pathUrls, nil
}
type pathUrl struct {
Path string `yaml:"path"`
URL string `yaml:"url"`
}
FULL ERROR
main/main.go:7:2: cannot find package "github.com/gophercises/urlshort" in any of:
/usr/lib/go-1.10/src/github.com/gophercises/urlshort (from $GOROOT)
/home/maylortaylor/src/github.com/gophercises/urlshort (from $GOPATH)
You need to fork the repo, not clone it directly.
Then create a new folder onto students directory
copy-paste the go files
your imports should look like "github.com/urlshort/students/{your name}"
Use the following comments to create a module in the root directory
go mod init [packageName]
then you can test by running
go run main/main.go