我已经创建了一个基于字符串的类型的角色,我现在试图通过实现Valuer和Scanner接口来使它与数据库驱动程序一起工作
type Role string
func (r *Role) Scan(value interface{}) error {
r = (*Role)(value.(string))
return nil
}
func (r *Role) Value(value driver.Value, err error) {
if err != nil {
value = string(r)
}
}
Run Code Online (Sandbox Code Playgroud)
我一直收到错误:
The Go code app/entities/user.go does not compile: cannot convert value.(string) (type string) to type *Role
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
例如:
{["NewYork",123]}
Run Code Online (Sandbox Code Playgroud)
因为json数组被解码为go数组,而go数组需要显式定义一个类型,我不知道如何处理它.
我是围棋新手。我正在使用天气 API。我已经注释掉了导致错误的部分。我看到其他几个链接也有类似的问题,但是它们似乎都没有在 JSON 字符串中间包含数组。我确信有一种方法可以用切片定义结构。我似乎无法获得允许它的语法。这是我被困住的地方:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
// WeatherData struct to collect data from the API call
type WeatherData struct {
Wind Wind
Sys Sys
// Weather Weather
Name string `json:"name"`
}
////////////// ERROR when unmarshalling this struct /////////
// Weather provides basic weather info
// type Weather struct {
// ID int `json:"id"`
// Descrip string `json:"description"`
// Icon string `json:"icon"`
// }
/////////////////////////////////////////////////////////////
// Sys includes sunrise, sunset, country, etc.
type …
Run Code Online (Sandbox Code Playgroud)