环境 / environment
$ go version
go version go1.18 windows/amd64
$ go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\chenchong\AppData\Local\go-build
set GOENV=C:\Users\chenchong\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\chenchong\.go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\chenchong\.go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.com.cn,direct
set GOROOT=D:\Developer\Tools\go1.18
set GOSUMDB=off
set GOTMPDIR=
set GOTOOLDIR=D:\Developer\Tools\go1.18\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.18
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\CHENCH~1\AppData\Local\Temp\go-build107498046=/tmp/go-build -gno-record-gcc-switches
问题 / issue
无法借助编译器得到更明确的指导信息
No more explicit guidance can be obtained from the compiler
代码 / code
node.go
package structure_list
type node[T any] struct {
prev *node[T] //前驱
data T //数据域
next *node[T] //后继
}
type linkedList[T any] struct {
data *node[T]
}
func (l *linkedList[T]) Add(data T) *linkedList[T] {
node := newNode(data)
if nil == l.data {
l.data = node
} else {
l.data.Put(data)
}
return l
}
func (l *linkedList[T]) AddAll(elements ...T) *linkedList[T] {
for _, v := range elements {
l.Add(v)
}
return l
}
func NewLinkedList[T any](elements ...T) *linkedList[T] {
list := &linkedList[T]{}
list.AddAll(elements...)
return list
}
func newNode[T any](data T) *node[T] {
node := &node[T]{data: data}
node.prev = node
node.next = node
return node
}
// Put 前插法
func (l *node[T]) Put(data T) *node[T] {
node := newNode(data)
// 1 <-> 2 <-> 3 <-> 1
// 如果 l 是 2 节点, next 就是 3
next := l.next
// 将 3 的前驱重定向到新的节点 4
// 1 <-> 2
// ... 4 <- 3 <-> 1
next.prev = node
// 将 4 的后继重定向到 3
// ... 4 <-> 3 <-> 1
node.next = next
// 将 4 的前驱重定向到 2
// 1 <-> 2 <- 4 <-> 3 <-> 1
node.prev = l
// 将 2 的后继重定向到 4
// 1 <-> 2 <-> 4 <-> 3 <-> 1
l.next = node
return l
}
func (l *node[T]) len(node *node[T], count int64) int64 {
if l == node {
return count
}
return l.len(node.next, count+1)
}
func (l *node[T]) Len() int64 {
next := l.next
return l.len(next, 1)
}
package structure_list
import (
"math/rand"
"testing"
)
func TestNewNode(t *testing.T) {
list := NewLinkedList[int](rand.Intn(50), rand.Intn(50), rand.Intn(50), rand.Intn(50), rand.Intn(50), rand.Intn(50))
t.Log(list)
t.Log()
}
go.mod
module structure_list
go 1.18
输出 / output
GOROOT=D:\Developer\Tools\go1.18 #gosetup
GOPATH=C:\Users\chenchong\.go #gosetup
D:\Developer\Tools\go1.18\bin\go.exe test -c -o C:\Users\chenchong\AppData\Local\Temp\GoLand\___structure_list__TestNewNode__1_.test.exe -gcflags "all=-N -l" structure_list #gosetup
# structure_list [structure_list.test]
<autogenerated>:1: internal compiler error: panic: runtime error: index out of range [0] with length 0
Please file a bug report including a short program that triggers the error.
https://go.dev/issue/new
求助 / help
代码经个人审视多次,暂时没有找到问题,现在不知道如何处理。还请相关了有遇到类似问题的提供解答 。
The code has been inspected for many times, but no problem has been found. Now I don't know how to deal with it. Please also provide solutions for those who have encountered similar problems.
环境 / environment
问题 / issue
无法借助编译器得到更明确的指导信息
No more explicit guidance can be obtained from the compiler
代码 / code
node.go
go.mod
输出 / output
求助 / help
代码经个人审视多次,暂时没有找到问题,现在不知道如何处理。还请相关了有遇到类似问题的提供解答 。
The code has been inspected for many times, but no problem has been found. Now I don't know how to deal with it. Please also provide solutions for those who have encountered similar problems.