相关疑难解决方法(0)

Golang类型断言

我已经创建了一个基于字符串的类型的角色,我现在试图通过实现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)

我在这做错了什么?

go

20
推荐指数
1
解决办法
3万
查看次数

如何在其中解组具有不同类型值的json数组

例如:

{["NewYork",123]}
Run Code Online (Sandbox Code Playgroud)

因为json数组被解码为go数组,而go数组需要显式定义一个类型,我不知道如何处理它.

json go

4
推荐指数
1
解决办法
1683
查看次数

将 json 数组解组到 go 结构中(数组位于 JSON 字符串的中间

我是围棋新手。我正在使用天气 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)

arrays json struct go unmarshalling

3
推荐指数
1
解决办法
2092
查看次数

标签 统计

go ×3

json ×2

arrays ×1

struct ×1

unmarshalling ×1