如何构建一个没有主函数的Go库

huangapple go评论96阅读模式
英文:

How to Build a GO Library without a main

问题

我是一名经验丰富的开发者,但对GO语言还不熟悉。我找到的文档不够直观。我习惯于使用Java、Scala和Python,但GO对我来说是新的。我正在尝试在GO中创建一个库,并且正在进行创建项目结构的第一步。当我运行go build时,它告诉我“没有GO文件...”。这是一个纯库,我不想要一个主文件。是否有办法实现这一点,或者无论如何都需要一个主文件?

我正在寻找文档并查看示例项目。
编辑。我确实有文件,并且已经使用一个主文件运行了一些测试。但是一旦我删除主文件,我就有点迷失在如何正确构建它的过程中。

英文:

I'm a seasoned developer but new to GO. The documentation I have found is not straightforward. I'm used to Java, Scala and Python, but GO is new to me. I'm trying to create a library in GO, and I'm in the first steps of creating the project structure. When I run go build it tells me there are "no GO files in ...". This is a pure library and I don't want a main file. Is there a way to achieve this or is a main file required no matter what?

Finding documentation and looking at sample projects.
Edit. I do have files and I have run some tests with a main file in place. But as soon as I delete the main file it's when I get a bit lost on how to build it correctly.

答案1

得分: 2

这取决于你对"library"的理解。如果你指的是预编译的二进制对象,比如C语言中的.a.so库,Go语言实际上没有类似的东西。依赖包会被下载为源代码,然后编译到你的二进制文件中。

如果你指的是"我可以在其他项目中导入和使用的模块"......你是否查看过其他模块的结构?以yaml模块为例,我们可以看到:

  • 没有main.go文件
  • 如果你克隆它并运行go mod tidy; go build命令,go build命令将不会报错。

因此,例如,我可以创建一个名为mymodule-dep的模块,具有以下结构:

.
├── functions.go
└── go.mod

其中go.mod文件包含:

module github.com/larsks/mymodule-dep

go 1.20

functions.go文件包含:

package mymodule_dep

import "strings"

func Replace(s string) string {
	return strings.Replace(s, "Hello", "Goodbye", -1)
}

我可以通过在另一个项目中导入该模块来使用它,例如:

package main

import (
	"fmt"
	dep "github.com/larsks/mymodule-dep"
)

func main() {
	fmt.Println(dep.Replace("Hello, world!"))
}

这假设存在一个名为<https://github.com/larsks/mymodule-dep>的代码仓库(目前确实存在)。

如果我构建上述代码并运行它,将会输出:

Goodbye, world!
英文:

It depends a bit on what you mean by "library". If you mean a pre-compiled binary object, like a .a or .so library in C, Go doesn't really have anything like that. Dependent packages are downloaded as source and then compiled into your binary.

If you mean, "a module I can import and use in other projects"...Have you looked to see how other modules are structured? Using e.g. the yaml module as an example, we see:

  • There is no main.go
  • If you clone it and run go mod tidy; go build the go build command will run without errors.

So for example I can create a module mymodule-dep with this structure:

.
├── functions.go
└── go.mod

Where go.mod has:

module github.com/larsks/mymodule-dep

go 1.20

And functions.go has:

package mymodule_dep

import &quot;strings&quot;

func Replace(s string) string {
	return strings.Replace(s, &quot;Hello&quot;, &quot;Goodbye&quot;, -1)
}

I can use that module by importing into another project, like this:

package main

import (
	&quot;fmt&quot;
	dep &quot;github.com/larsks/mymodule-dep&quot;
)

func main() {
	fmt.Println(dep.Replace(&quot;Hello, world!&quot;))
}

This assumes that there exists a repository <https://github.com/larsks/mymodule-dep> (which does exist right now).

If I build the above code, running it produces:

Goodbye, world!

答案2

得分: 1

通常情况下,你不需要构建你的GO库。

你可以创建GO文件作为库,对其进行测试,并将源代码发布到GitHub上。发布的代码在Git(通常是GitHub)中有版本,客户端可以选择使用哪个版本的库。

使用该库的客户端应用程序有一个main.go文件,你可以在其中运行go build命令。

Go语言没有像Java或.NET那样的中间格式,也不使用二进制可执行文件中的库。相反,Go使用GO代码形式的依赖项。这与Node和Python类似,不同的是,Go会编译最终的代码而不是解释它。

英文:

Usually you do not need to build your GO libraries.

You create library as GO files, test it and publish sources to GitHub. Published code has versions in Git (typically GitHub) and client can pick what version of library to use.

The client application that uses that library has a main.go file, and that is where you run go build command.

Go does not have an intermediate format like Java or .NET and also do not use libraries in binary executable format. Instead, GO uses dependencies in the form of GO code. That is similar to Node and Python, except, Go compiles final code and do not interpret it.

huangapple
  • 本文由 发表于 2023年7月9日 07:58:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76645384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定