在一般情况下,我不能优雅地将图像的像素作为数组.
f, err := os.Open(imgPath)
check(err)
defer f.Close()
img, _, err := image.Decode(bufio.NewReader(f))
check(err)
pixels, err := getPixels(img)
check(err)
// Logic with pixels.
Run Code Online (Sandbox Code Playgroud)
现在函数getPixels看起来像这样:
func getPixels(img image.Image) ([]uint8, error) {
if i, ok := img.(*image.NRGBA); ok {
return i.Pix, nil
} else if i, ok := img.(*image.Alpha); ok {
return i.Pix, nil
} else if i, ok := img.(*image.Alpha16); ok {
return i.Pix, nil
} else if i, ok := img.(*image.CMYK); ok {
return i.Pix, nil
} else if i, …Run Code Online (Sandbox Code Playgroud) 我有这样的界面:
type ViewInterface interface{
Init() View
}
type View struct{
Width int
Height int
}
Run Code Online (Sandbox Code Playgroud)
所以我从View创建了一个新类型
type MainView View
func (m MainView) Init() MainView{
return MainView{
Width:10,
Height:10,
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将MainView传递给以下方法:
func Render(views ...ViewInterface){
for _, view := range views {
v := view.Init()
}
}
func main() {
Render(MainView{})
}
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
不能使用MainView文字(类型MainView)作为Render参数中的类型ViewInterface:MainView没有实现ViewInterface(Init方法的错误类型)
有Init()MainView
想要Init()视图
为什么MianView不一样View?解决这个问题的正确方法是什么?
谢谢
我是golang的新手.我需要设计一个函数来根据输入创建不同类型的对象.但我没有弄清楚如何设计界面.这是我的代码:
package main
import (
"fmt"
)
type AA struct{
name string
}
func (this *AA) say(){
fmt.Println("==========>AA")
}
type BB struct{
*AA
age int
}
func (this *BB) say(){
fmt.Println("==========>BB")
}
func ObjectFactory(type int) *AA {
if type ==1 {
return new(AA)
}else{
return new(BB)
}
}
func main() {
obj1 := ObjectFactory(0)
obj1.say()
obj2 := ObjectFactory(0)
obj2.say()
}
Run Code Online (Sandbox Code Playgroud)
无论我问ObjectFactory返回*AA还是接口{},编译器都会告诉我错误.我怎样才能使它工作?
我有以下代码:
package main
import (
"log"
)
type Data struct {
Id int
Name string
}
type DataError struct {
Message string
ErrorCode string
}
func main() {
response := Data{Id: 100, Name: `Name`}
if true {
response = DataError{Message: `message`, ErrorCode: `code`}
}
log.Println(response)
}
Run Code Online (Sandbox Code Playgroud)
此代码返回一个错误:
./start.go:20: 不能使用 DataError 文字(类型 DataError)作为赋值中的类型数据
似乎我无法分配给response不同类型的 var 数据(在我的情况下DataError)。我听说可能的解决方案是通过接口统一Data和DataError结构化。或者也许有另一个更好的解决方案?
你能指出我如何解决这个问题吗?
谢谢
我是 Go 的新手,我想知道 Goland 或带有 CLI 工具的方法是否可以列出实现给定接口的所有类型。
我找到了一个名为的工具guru,可以列出给定类型实现的所有接口,但我无法在 CLI 中使用它。
我有一个经典的 Go nil 接口问题。
我试图断言 an interface{},我从 a 分配nil error回一个error接口。这句话很令人困惑,所以我有一个方便的例子: https: //play.golang.com/p/Qhv7197oIE_z
package main
import (
"fmt"
)
func preferredWay(i interface{}) error {
return i.(error)
}
func workAround(i interface{}) error {
if i == nil {
return nil
}
return i.(error)
}
func main() {
var nilErr error
fmt.Println(workAround(nilErr)) // Prints "<nil>" as expected.
fmt.Println(preferredWay(nilErr)) // Panics.
}
Run Code Online (Sandbox Code Playgroud)
输出:
<nil>
panic: interface conversion: interface is nil, not error
goroutine 1 [running]:
main.preferredWay(...)
/tmp/sandbox415300914/prog.go:8
main.main()
/tmp/sandbox415300914/prog.go:21 …Run Code Online (Sandbox Code Playgroud) 编辑:这不是在 Go 中使用接口的正确方法。这个问题的目的是让我了解空接口在 Go 中是如何工作的。
如果 Go 中的所有类型都实现了interface{}(空接口),为什么我不能访问and结构中的name字段?如何通过函数 sayHi() 访问每个结构体的名称字段?CatDog
package main
import (
"fmt"
)
func sayHi(i interface{}) {
fmt.Println(i, "says hello")
// Not understanding this error message
fmt.Println(i.name) // i.name undefined (type interface {} is interface with no methods)
}
type Dog struct{
name string
}
type Cat struct{
name string
}
func main() {
d := Dog{"Sparky"}
c := Cat{"Garfield"}
sayHi(d) // {Sparky} says hello
sayHi(c) // {Garfield} …Run Code Online (Sandbox Code Playgroud) 我有这个代码。我希望接口的类型断言为 int。但是,接口的类型改为 float64。谁能解释为什么会这样?以及规避它的最佳方法是什么。
package main
import (
"fmt"
"encoding/json"
)
type obj struct {
X interface{}
}
func main() {
var x int
x = 5
o := &obj {
X: x,
}
b, _ := json.Marshal(o)
var newObj obj
json.Unmarshal(b, &newObj)
if _, ok := newObj.X.(int); ok {
fmt.Println("X is an int")
} else if _, ok := newObj.X.(float64); ok {
fmt.Println("X is a float64")
} else {
fmt.Println("This does not make any sense")
}
}
Run Code Online (Sandbox Code Playgroud)
此代码打印“X 是一个 float64”。您可以在那里运行代码 …
我有一个变量数据,它是一个接口。当我打印其类型时,将其获取为json.Number。如何将强制类型转换为int / int64 / float64
如果我尝试数据。(float64),最终会出现紧急错误
panic: interface conversion: interface {} is json.Number, not float64
Run Code Online (Sandbox Code Playgroud) 有人可以向我解释为什么在 go 中不允许这种实现吗?我有一个函数,它将函数定义的接口作为参数。这会引发错误。
package main
import (
"fmt"
)
type Anode int
func (a Anode) IsLess(node Anode) bool { return a < node }
type Node interface {
IsLess(node Node) bool
}
func test(a, b Node) bool {
return a.IsLess(b)
}
func main() {
a := Anode(1)
b := Anode(2)
fmt.Println(test(a, b))
}
Run Code Online (Sandbox Code Playgroud) 当我尝试使用以下代码段将map[string]string对象转换为map[string]interface{}golang 时,出现错误。
package main
import "fmt"
func main() {
var m = make(map[string]string)
m["a"] = "b"
m1 := map[string]interface{}(m)
fmt.Println(m1)
}
Run Code Online (Sandbox Code Playgroud)
我收到这样的错误:
# example
./prog.go:10:30: cannot convert m (type map[string]string) to type map[string]interface {}
Run Code Online (Sandbox Code Playgroud)
我可以使用以下代码使用 long for 循环解决方案来转换它,但我想知道是否有更简单的方法。
package main
import (
"fmt"
)
func main() {
m := map[string]string{
"a": "a",
"b": "b",
}
m2 := make(map[string]interface{}, len(m))
for k, v := range m {
m2[k] = v
}
fmt.Println(m2)
}
Run Code Online (Sandbox Code Playgroud)