I
I
inbider2016-08-07 12:40:27
go
inbider, 2016-08-07 12:40:27

How to pass a pointer to a structure to a function of another package?

Hello! Faced such a problem, it is impossible to pass a pointer to a structure to a function from another package.
Own code example:

package main
import "Project01/tools"
type TestTest struct {
    Str1,Str2,Str3 string
}
func main() {
    var par1 TestTest
    tools.Foo(&par1)
}

And the tools package
package tools
import "fmt"
func Foo(par *TestTest) { // <- Тут ругается, пишет не знаю такой структуры TestTest
    fmt.Println(par)
}

Tell me what's the catch? How to fix?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
#
#algooptimize #bottize, 2016-08-07
@inbider

Your tools doesn't know anything about TestTest from main.
It makes more sense to declare it in tools. Or use interface{}

N
Nikita, 2016-08-07
@bitver

Import package?
PS People, stop writing in notepads, there are normal IDEs, and there are hundreds of them. 99% of them will not allow this and import the package automatically. This is the most elementary thing, but what will happen next?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question