Go构建过程似乎错误地推断出了错误的Go版本。

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

Go build process seems to erroneously infer the wrong version of Go:

问题

我已经为您翻译了内容,请查看以下翻译结果:

我已经构建了一个简单的库来在Go中运行一个工作池,并且有一个文件(用于测试)使用了该库(请参见下面的代码)。

当我使用库的本地版本(存储在$GOROOT/src/...中)时,使用以下代码:

import (
	"fmt"
	"sync"

	. "go_parallel"
)

它可以正常工作,但是当我使用存储在GitHub上的副本时,使用以下代码:

import (
	"fmt"
	"sync"

	. "github.com/serge-hulne/go_parallel"
)

我会收到以下错误信息:

sergehulne@Serges-Mac-mini parallel_2 % go build                                 
# github.com/serge-hulne/go_parallel
../../../../go/bin/pkg/mod/github.com/serge-hulne/go_parallel@v0.0.0-20220325154855-13fe54ffe85c/parallel.go:7:23: type parameter requires go1.18 or later (-lang was set to go1.16; check go.mod)
../../../../go/bin/pkg/mod/github.com/serge-hulne/go_parallel@v0.0.0-20220325154855-13fe54ffe85c/parallel.go:7:25: undeclared name: any (requires version go1.18 or later)
../../../../go/bin/pkg/mod/github.com/serge-hulne/go_parallel@v0.0.0-20220325154855-13fe54ffe85c/parallel.go:7:54: type instantiation requires go1.18 or later (-lang was set to go1.16; check go.mod)
../../../../go/bin/pkg/mod/github.com/serge-hulne/go_parallel@v0.0.0-20220325154855-13fe54ffe85c/parallel.go:9:13: type parameter requires go1.18 or later (-lang was set to go1.16; check go.mod)
../../../../go/bin/pkg/mod/github.com/serge-hulne/go_parallel@v0.0.0-20220325154855-13fe54ffe85c/parallel.go:9:15: undeclared name: any (requires version go1.18 or later)
../../../../go/bin/pkg/mod/github.com/serge-hulne/go_parallel@v0.0.0-20220325154855-13fe54ffe85c/parallel.go:14:19: type parameter requires go1.18 or later (-lang was set to go1.16; check go.mod)
../../../../go/bin/pkg/mod/github.com/serge-hulne/go_parallel@v0.0.0-20220325154855-13fe54ffe85c/parallel.go:14:21: undeclared name: any (requires version go1.18 or later)
../../../../go/bin/pkg/mod/github.com/serge-hulne/go_parallel@v0.0.0-20220325154855-13fe54ffe85c/parallel.go:14:67: type instantiation requires go1.18 or later (-lang was set to go1.16; check go.mod)

go.mod文件:

module x

go 1.18

require github.com/serge-hulne/go_parallel v0.0.0-20220325154855-13fe54ffe85c // indirect

Go版本:

go version go1.18 darwin/arm64

库文件:

package go_parallel

import (
	"sync"
)

type ParallelCallback[T any] func(chan T, chan Result[T], int, *sync.WaitGroup)

type Result[T any] struct {
	Id    int
	Value T
}

func Run_parallel[T any](n_workers int, in chan T, out chan Result[T], Worker ParallelCallback[T]) {
	go func() {
		wg := sync.WaitGroup{}
		defer close(out) // close the output channel when all tasks are completed
		for id := 0; id < n_workers; id++ {
			wg.Add(1)
			go Worker(in, out, id, &wg)
		}
		wg.Wait() // wait for all workers to complete their tasks *and* trigger the -differed- close(out)
	}()
}

使用该库的代码:

// Example of use

package main

import (
	"fmt"
	"sync"

	//. "go_parallel"
	. "github.com/serge-hulne/go_parallel"
)

// Example of use:
const (
	NW         = 8
	BufferSize = 1
)

func Worker(in chan int, out chan Result[int], id int, wg *sync.WaitGroup) {
	defer wg.Done()
	for item := range in {
		item *= 2 // returns the double of the input value (Bogus handling of data)
		out <- Result[int]{Id: id, Value: item}
	}
}

func main() {

	// in and out channels:
	in := make(chan int, BufferSize)
	out := make(chan Result[int])

	// Populate in channel (send data to input stream)
	go func() {
		defer close(in)
		for i := 0; i < 10; i++ {
			in <- i
		}
	}()

	// Run tasks (Worker func) in parallel:
	Run_parallel(NW, in, out, Worker)

	// Display results:
	for item := range out {
		fmt.Printf("From out [%d]: %d\n", item.Id, item.Value)
	}
	println("- - - All done - - -")
}

基本上,我的问题是:为什么构建过程会假设一个Go的版本,而这个版本似乎取决于我是使用本地副本还是远程副本(GitHub)的相同代码?

英文:

I have built a simple library to run a pool of workers in Go and a file (to test it) which uses said library, (see code hereunder).

When I use the local version of the library (stored in $GOROOT/src/...), using:

import (
	&quot;fmt&quot;
	&quot;sync&quot;

	. &quot;go_parallel&quot;

)

it works fine, but when I use my copy stored on GitHub, using:

import (
	&quot;fmt&quot;
	&quot;sync&quot;

	. &quot;github.com/serge-hulne/go_parallel&quot;
)

I get the error message:

sergehulne@Serges-Mac-mini parallel_2 % go build                                 
# github.com/serge-hulne/go_parallel
../../../../go/bin/pkg/mod/github.com/serge-hulne/go_parallel@v0.0.0-20220325154855-13fe54ffe85c/parallel.go:7:23: type parameter requires go1.18 or later (-lang was set to go1.16; check go.mod)
../../../../go/bin/pkg/mod/github.com/serge-hulne/go_parallel@v0.0.0-20220325154855-13fe54ffe85c/parallel.go:7:25: undeclared name: any (requires version go1.18 or later)
../../../../go/bin/pkg/mod/github.com/serge-hulne/go_parallel@v0.0.0-20220325154855-13fe54ffe85c/parallel.go:7:54: type instantiation requires go1.18 or later (-lang was set to go1.16; check go.mod)
../../../../go/bin/pkg/mod/github.com/serge-hulne/go_parallel@v0.0.0-20220325154855-13fe54ffe85c/parallel.go:9:13: type parameter requires go1.18 or later (-lang was set to go1.16; check go.mod)
../../../../go/bin/pkg/mod/github.com/serge-hulne/go_parallel@v0.0.0-20220325154855-13fe54ffe85c/parallel.go:9:15: undeclared name: any (requires version go1.18 or later)
../../../../go/bin/pkg/mod/github.com/serge-hulne/go_parallel@v0.0.0-20220325154855-13fe54ffe85c/parallel.go:14:19: type parameter requires go1.18 or later (-lang was set to go1.16; check go.mod)
../../../../go/bin/pkg/mod/github.com/serge-hulne/go_parallel@v0.0.0-20220325154855-13fe54ffe85c/parallel.go:14:21: undeclared name: any (requires version go1.18 or later)
../../../../go/bin/pkg/mod/github.com/serge-hulne/go_parallel@v0.0.0-20220325154855-13fe54ffe85c/parallel.go:14:67: type instantiation requires go1.18 or later (-lang was set to go1.16; check go.mod)

go.mod:

module x

go 1.18

require github.com/serge-hulne/go_parallel v0.0.0-20220325154855-13fe54ffe85c // indirect

Go version:

go version go1.18 darwin/arm64

Library

package go_parallel

import (
	&quot;sync&quot;
)

type ParallelCallback[T any] func(chan T, chan Result[T], int, *sync.WaitGroup)

type Result[T any] struct {
	Id    int
	Value T
}

func Run_parallel[T any](n_workers int, in chan T, out chan Result[T], Worker ParallelCallback[T]) {
	go func() {
		wg := sync.WaitGroup{}
		defer close(out) // close the output channel when all tasks are completed
		for id := 0; id &lt; n_workers; id++ {
			wg.Add(1)
			go Worker(in, out, id, &amp;wg)
		}
		wg.Wait() // wait for all workers to complete their tasks *and* trigger the -differed- close(out)
	}()
}

Code using said library

// Example of use

package main

import (
	&quot;fmt&quot;
	&quot;sync&quot;

	//. &quot;go_parallel&quot;
	. &quot;github.com/serge-hulne/go_parallel&quot;
)

// Example of use:
const (
	NW         = 8
	BufferSize = 1
)

func Worker(in chan int, out chan Result[int], id int, wg *sync.WaitGroup) {
	defer wg.Done()
	for item := range in {
		item *= 2 // returns the double of the input value (Bogus handling of data)
		out &lt;- Result[int]{Id: id, Value: item}
	}
}

func main() {

	// in and out channels:
	in := make(chan int, BufferSize)
	out := make(chan Result[int])

	// Populate in channel (send data to input stream)
	go func() {
		defer close(in)
		for i := 0; i &lt; 10; i++ {
			in &lt;- i
		}
	}()

	// Run tasks (Worker func) in parallel:
	Run_parallel(NW, in, out, Worker)

	// Display results:
	for item := range out {
		fmt.Printf(&quot;From out [%d]: %d\n&quot;, item.Id, item.Value)
	}
	println(&quot;- - - All done - - -&quot;)
}

Basically my question is : Why does the build process assume a version of go which appears to vary depending upon whether I use a local copy or a remote copy (gitHub) of the same code?

答案1

得分: 1

解决方案是:

源文件需要有自己的 go.mod 文件(用于将包装在一个共同的模块中)。其中一个目的是指定库使用的 go 版本。

在这个例子中,go_parallel 使用了泛型,所以需要 go 1.18。

最初,客户端模块 module x 在其 go.mod 文件中指定了 go 1.18,但是在 github.com/serge-hulne/go_parallel 仓库中使用泛型的代码没有模块,因此默认为 go 1.16,如上面的评论中 JimB 所述。

解决方案是在 github.com/serge-hulne/go_parallel 仓库中添加一个 go.mod 文件,并确保它指定了 go 1.18。go.mod 内容如下:

module github.com/serge-hulne/go_parallel

go 1.18
英文:

The solution is :

The source files require a go.mod file of their own (to wrap the packages in a common module). One of its purposes is to specify the go version used by the library.

In this instance go 1.18 is required because go_parallel uses generics.

Originally, the client module module x specified go 1.18 in its go.mod file, but the code using generics in the github.com/serge-hulne/go_parallel repo did not have a module, and hence it defaulted to go 1.16 as described by JimB in comment above.

The solution was to add a go.mod file to the github.com/serge-hulne/go_parallel repo, and make sure it specified go 1.18. go.mod content:

module github.com/serge-hulne/go_parallel

go 1.18

huangapple
  • 本文由 发表于 2022年3月26日 00:33:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/71620266.html
匿名

发表评论

匿名网友

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

确定