同一个package中函数互相调用为undefined的解决

代码如下:

package main

import "fmt"

func main() {
    myFunc()
}

func init() {
    fmt.Println("there is main.init()")
}
package main

import "fmt"

func init() {
    fmt.Println("there is myClass.init()")
}

func myFunc() {
    fmt.Println("there is myClass.myFunc()")
}

执行main方法后得到:

Go语言中同一个package中函数互相调用为undefined的解决

解决方案

GoLand通过option+F12打开Terminal终端执行一下操作:

go run *.go

或者

go build .
./run
// 恕我直言,这种方法还没成功

同一个package下不同文件中函数调用报未定义问题

代码如下:

  • hello.go文件
package main

import "fmt"

func hello()  {
   fmt.Print("go =======")
}
  • main.go文件
package main

func main() {
    hello()
}

编译过程报错:

# command-line-arguments
test\main.go:10:3: undefined: hello


Process finished with exit code 2

出现这样的原因是hello.go文件并未编译

解决方式大致有两种

  • 第一个:通过go命令编译,go run *.go 或 (go bulid . ,. run)
  • 第二个:通过单元测试调用函数

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文地址:https://blog.csdn.net/weixin_38204723/article/details/81068811

相关文章:

  • 2023-03-19
  • 2022-12-23
  • 2022-02-05
  • 2021-06-12
  • 2022-12-23
  • 2021-09-29
猜你喜欢
  • 2022-12-23
  • 2021-10-03
  • 2021-07-20
  • 2022-12-23
  • 2021-09-11
  • 2021-12-10
相关资源
相似解决方案