S
S
Sergei Abramov2020-09-15 21:18:42
go
Sergei Abramov, 2020-09-15 21:18:42

Why are modules not being imported?

I don't understand how to import modules correctly. Here is a demo I posted:
main.go at the root:

package main

import "./client"

func main() {
  client.TestFunc("Test")
}


And ./client/client.go :
package client

import (
  "fmt"
  "github.com/gorilla/websocket"
)

type Client struct {
  Name string
  conn *websocket.Conn
}

func TestFunc(name string) {
  c := &Client{Name: name}
  fmt.Printf("%s", c.Name)
}


go.mod file:
module test
go 1.14
require github.com/gorilla/websocket v1.4.2

I run : go run main.go and get an error: " client/client.go:5:2: cannot find package ", i.e. it doesn't see github.com/gorilla/websocket in imports. What am I doing wrong? Or should there be a go.mod file for every package? I use package to logically separate the type of namespaces, so that functions belong to the same "class". And go.mod thought it would be like a package manager for the entire application

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Pavlyuk, 2020-09-16
@PatriotSY

import "./client"
You can't do that, the import path is always absolute. If your app's module in go.mod is called test, then the import will be test/client

C
cython, 2020-09-15
@cython

In the go.mod folder, run go build. Also, before using the package, you need to download it via go get

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question