英文:
Golang OOP architecture, passing a slice into a constructor, creating a slice object
问题
我刚刚开始学习go
语言,一切看起来都很酷,我编写了我需要的所有代码,但是我遇到了两个问题,实际上是将一个切片传递给一个Struct
或者所谓的object
,以及如何创建一个方法,例如我可以添加另一个矩阵,但我认为这是一个较小的问题。
代码:
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
a := Matrix(nonsingularRandomMatrix())
fmt.Println(a)
}
type Matrix struct {
inputMatrix [][]int
}
func NewThing(inMatrix [][]int) *Matrix {
p := new(Matrix)
p.inputMatrix = inMatrix
return p
}
func nonsingularRandomMatrix() [][]int {
rand.Seed(time.Now().UTC().UnixNano())
var matrixDeterminant int = 0
nm := make([][]int, 4)
for i := 0; i < 4; i++ {
nm[i] = make([]int, 4)
for j := 0; j < 4; j++ {
nm[i][j] = rand.Intn(100)
}
}
matrixDeterminant =
nm[0][0]*nm[1][1]*nm[2][2]*nm[3][3] - nm[0][0]*nm[1][1]*nm[2][3]*nm[3][2] -
nm[0][0]*nm[1][2]*nm[2][1]*nm[3][3] + nm[0][0]*nm[1][2]*nm[2][3]*nm[3][1] +
nm[0][0]*nm[1][3]*nm[2][1]*nm[3][2] - nm[0][0]*nm[1][3]*nm[2][2]*nm[3][1] -
nm[0][1]*nm[1][0]*nm[2][2]*nm[3][3] + nm[0][1]*nm[1][0]*nm[2][3]*nm[3][2] +
nm[0][1]*nm[1][2]*nm[2][0]*nm[3][3] - nm[0][1]*nm[1][2]*nm[2][3]*nm[3][0] -
nm[0][1]*nm[1][3]*nm[2][0]*nm[3][2] + nm[0][1]*nm[1][3]*nm[2][2]*nm[3][0] +
nm[0][2]*nm[1][0]*nm[2][1]*nm[3][3] - nm[0][2]*nm[1][0]*nm[2][3]*nm[3][1] -
nm[0][2]*nm[1][1]*nm[2][0]*nm[3][3] + nm[0][2]*nm[1][1]*nm[2][3]*nm[3][0] +
nm[0][2]*nm[1][3]*nm[2][0]*nm[3][1] - nm[0][2]*nm[1][3]*nm[2][1]*nm[3][0] -
nm[0][3]*nm[1][0]*nm[2][1]*nm[3][2] + nm[0][3]*nm[1][0]*nm[2][2]*nm[3][1] +
nm[0][3]*nm[1][1]*nm[2][0]*nm[3][2] - nm[0][3]*nm[1][1]*nm[2][2]*nm[3][0] -
nm[0][3]*nm[1][2]*nm[2][0]*nm[3][1] + nm[0][3]*nm[1][2]*nm[2][1]*nm[3][0]
if matrixDeterminant == 0 {
nonsingularRandomMatrix()
}
return nm
}
我的控制台输出:
> go run oop.go
>
> command-line-arguments
>
> .\oop.go:10: 无法将nonsingularRandomMatrix() (类型为[][]int) 转换为类型Matrix
有什么线索吗?
问候!
英文:
I just started with go
language, everything looks cool, I coded everything I need but I have two problems, actually it is passing an slice into a Struct
or so called object
, and how to create a method that I can add for example another matrix, but this is minor problem I think.
Code:
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
a := Matrix(nonsingularRandomMatrix())
fmt.Println(a)
}
type Matrix struct {
inputMatrix [][]int
}
func NewThing(inMatrix [][]int) *Matrix {
p := new(Matrix)
p.inputMatrix = inMatrix
return p
}
func nonsingularRandomMatrix() [][]int {
rand.Seed(time.Now().UTC().UnixNano())
var matrixDeterminant int = 0
nm := make([][]int, 4)
for i := 0; i < 4; i++ {
nm[i] = make([]int, 4)
for j := 0; j < 4; j++ {
nm[i][j] = rand.Intn(100)
}
}
matrixDeterminant =
nm[0][0]*nm[1][1]*nm[2][2]*nm[3][3] - nm[0][0]*nm[1][1]*nm[2][3]*nm[3][2] -
nm[0][0]*nm[1][2]*nm[2][1]*nm[3][3] + nm[0][0]*nm[1][2]*nm[2][3]*nm[3][1] +
nm[0][0]*nm[1][3]*nm[2][1]*nm[3][2] - nm[0][0]*nm[1][3]*nm[2][2]*nm[3][1] -
nm[0][1]*nm[1][0]*nm[2][2]*nm[3][3] + nm[0][1]*nm[1][0]*nm[2][3]*nm[3][2] +
nm[0][1]*nm[1][2]*nm[2][0]*nm[3][3] - nm[0][1]*nm[1][2]*nm[2][3]*nm[3][0] -
nm[0][1]*nm[1][3]*nm[2][0]*nm[3][2] + nm[0][1]*nm[1][3]*nm[2][2]*nm[3][0] +
nm[0][2]*nm[1][0]*nm[2][1]*nm[3][3] - nm[0][2]*nm[1][0]*nm[2][3]*nm[3][1] -
nm[0][2]*nm[1][1]*nm[2][0]*nm[3][3] + nm[0][2]*nm[1][1]*nm[2][3]*nm[3][0] +
nm[0][2]*nm[1][3]*nm[2][0]*nm[3][1] - nm[0][2]*nm[1][3]*nm[2][1]*nm[3][0] -
nm[0][3]*nm[1][0]*nm[2][1]*nm[3][2] + nm[0][3]*nm[1][0]*nm[2][2]*nm[3][1] +
nm[0][3]*nm[1][1]*nm[2][0]*nm[3][2] - nm[0][3]*nm[1][1]*nm[2][2]*nm[3][0] -
nm[0][3]*nm[1][2]*nm[2][0]*nm[3][1] + nm[0][3]*nm[1][2]*nm[2][1]*nm[3][0]
if matrixDeterminant == 0 {
nonsingularRandomMatrix()
}
return nm
}
Output from my console:
> go run oop.go
>
> command-line-arguments
>
> .\oop.go:10: cannot convert nonsingularRandomMatrix() (type [][]int) to type Matrix
Any clue?
Greets!
答案1
得分: 3
你的编译器的意思是[][]int
和Matrix
是两个完全不相关的类型。你不能从一个类型转换为另一个类型。这是因为Matrix
是一个包含[][]int
的结构体,但Matrix
本身并不是[][]int
。要创建一个Matrix
,你需要创建一个新的Matrix
并将[][]int
设置为其唯一成员:
a := Matrix{nonsingularRandomMatrix()}
更简单的方法是将Matrix
定义为[][]int
的别名:
type Matrix [][]int
这将为[][]int
创建一个新的名称Matrix
,在这种情况下,可以使用简单的(显式)转换:
Matrix(nonsingularRandomMatrix())
英文:
What your compiler is saying is that [][]int
and Matrix
are two totally unrelated types. You can not convert from one to the other. Thats because Matrix
is a struct
which contains a [][]int
, but Matrix
is not an [][]int
. To create a Matrix
out of it you'd need to create a new Matrix
and set the [][]int
to the only member:
a := Matrix{nonsingularRandomMatrix()}
An easier way would be if Matrix
were a [][]int
.
type Matrix [][]int
This would just create a new name (Matrix
) for [][]int
, and in this case, a simple (explicit) conversion would work
Matrix(nonsingularRandomMatrix())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论