douliu8327 2012-05-02 15:22
浏览 1066
已采纳

在Go中构建并引用我自己的本地包

I'm playing with Google Go and I'm having fun (!), but I'm having some problems with package subsystem.

I'm running Go 1.0.1 on Mac OS X Lion. I've build also various single file programs without problems (I've also build a small webapp using html/templates without problems and it compiles and runs without any error).

I've defined a "reusable" package (even.go):

package even

func Even(i int) bool {
    return i % 2 == 0
}

func Odd(i int) bool {
    return i % 2 == 1
}

and a consumer program (useeven.go):

package main

import (
    "./even"
    "fmt"
)

func main() {
    a := 5
    b := 6

    fmt.Printf("%d is even %v?
", a, even.Even(a))
    fmt.Printf("%d is odd %v?
", b, even.Odd(b))
}

But when I compile the "library" using

go build even.go

I got nothing... No errors, no message... What happens?

How should I do this?

  • 写回答

4条回答 默认 最新

  • dongxi7609 2012-05-02 16:18
    关注

    The answer to your question, "How should I do this?" is explained in How to Write Go Code. It's really pretty important stuff and worth taking a look at.

    The behavior of go build might seem puzzling, but is actually conventional for command line programs--no output means that the program ran successfully. So what did it do? For that your answer is in go help build

    ... Otherwise build compiles the packages but discards the results, serving only as a check that the packages can be built.

    What, you wanted more? Of course. "How to Write Go Code" explains good ways of doing this. For a quick fix to your program, I'll explain that the go command expects each package and each executable program to be in a separate directory. If you just make a directory called even, immediately under the location of useeven.go, and move even.go to it, then go run useeven.go should run just as you have it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器