dry18813 2014-12-05 11:09
浏览 117

如何在同一应用程序中使用多个.go文件

Good Morning All,

I am new to Golang. I want to move some of my functions out into separate files so that I will not have like a 10,000 line .go file at the end. lol. I created two files both have the same package called main. Do I need to change package name to be app specific? Anyway how do I get these two files to talk?

Example:

MainFile.go:

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello World!")

    Test()
}

NewFile.go:

package main

import (
    "fmt"
)

func Test() {
    fmt.Println("Hello World Again!")
}

The test method is in the second file but cannot be reached by the first. I am sure this is some rudimentary thing I am missing.

Thanks

Update: I tried specifying this on the command line: go build MainFile.go NewSourceFile.go. It comes back with no errors but never builds the binary. How do I get it to output the binary now?

  • 写回答

1条回答 默认 最新

  • dsasd12314 2014-12-05 12:43
    关注

    If you run go run MainFile.go, Test() won't be found because its not in that file. You have to build the package then run the package:

    Inside the folder where the 2 files are, run go build and you will get a binary in that folder. Then just run the binary: ./MyPackage

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化