S
S
Space Around2021-02-06 17:23:41
go
Space Around, 2021-02-06 17:23:41

Why is the function defined in another file, but in the same package, not visible?

I defined a function in the driver.go file, I use it inside the test in driver_test.go, these two files are part of the same package, but when I run go test -v ./driver_test it gives an error that the function is not defined (undefined). I tried Google, there are similar problems, but the solutions did not help, I also looked at the go documentation

driver.go

package cowrie

// Drty is a func to handle Drty request
func Drty(p1 string, p2 string) {
  fmt.Println("1234")
}


driver_test.go
package cowrie

import (
  "testing"
)

func Test_Deposit(t *testing.T) {
  Drty("1", "2")
}


log errors
# command-line-arguments [command-line-arguments.test]
wire-transfer\cowrie\driver_test.go:9:7: undefined: Driver
FAIL    command-line-arguments [build failed]
FAIL

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Mamonov, 2021-02-06
@EvgenyMamonov

The essence of the problem is that you specify only the file with the test, but you also need to specify the file with the declaration and implementation (if they are different).
This is how it will work: I suspect that you really wanted to make sure that not all the tests in the package were executed, but a specific one, if so, then it’s easier to do it like this (I often do this myself :)))
go test -v ./driver_test.go ./driver.go
go test -v -run ^Test_Deposit$

V
Vladislav, 2021-02-06
@ghostiam

go test -v .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question