【问题标题】:uint8 to literal string representationuint8 到文字字符串表示
【发布时间】:2020-01-15 12:48:53
【问题描述】:

我正在尝试使用给定 ID 22024283 访问 HackerNews API 端点,该 ID 代表特定新闻项目,例如 https://hacker-news.firebaseio.com/v0/22024283.json

此 itemID 的类型为 uint8,我需要将其转换为 string 表示形式以插入 URL。

我不能使用strconv.Itoa(int(id)),因为它会产生数字 91 而不会保留 22024283。

对此的任何帮助将不胜感激。到目前为止,这是我的代码,感兴趣的功能是GetHackerNewsItem()


import (
    "fmt"
    "io/ioutil"
    "net/http"
    "strconv"
    "time"
)

//Client represents connection to firebase datastore
type Client struct {
    BASEURI string
    Version string
    Suffix  string
}
type Item struct {
    id          int       `json:"id"`
    itemtype    string    `json:"itemtype"`
    by          string    `json:"by"`
    time        time.Time `json:"time"`
    kids        []int     `json:"kids"`
    url         string    `json:"url"`
    score       int       `json:"score"`
    text        string    `json:"text"`
    title       string    `json:"title"`
    descendants int       `json:"descendants"`
}

//Connect to firebase datastore
func NewHackerNewsClient() *Client {
    var client Client
    client.BASEURI = "https://hacker-news.firebaseio.com/"
    client.Version = "v0"
    client.Suffix = ".json"
    return &client
}
func MakeHTTPRequest(url string) ([]byte, error) {
    response, err := http.Get(url)
    if err != nil {
        fmt.Printf("The http request failed with the error %s\n", err)
    }
    body, err := ioutil.ReadAll(response.Body)
    if err != nil {
        fmt.Printf("Failed to read response data with the error %s\n", err)
        return nil, err
    }
    return body, nil

}

func (client *Client) GetHackerNewsItem(id uint8) []byte {
    itemID := strconv.Itoa(int(id))
    fmt.Printf(itemID)
    url := client.BASEURI + client.Version + itemID + client.Suffix
    fmt.Printf(url)
    item, _ := MakeHTTPRequest(url)
    fmt.Print(item)
    return item
}
func (client *Client) GetTopStories() {

    url := client.BASEURI + client.Version + "/topstories/" + client.Suffix
    itemArray, _ := MakeHTTPRequest(url)
    for i := 0; i < len(itemArray); i++ {
        item := client.GetHackerNewsItem(itemArray[i])
        fmt.Print(item)
    }
}

func main() {
    client := NewHackerNewsClient()
    client.GetTopStories()

}

【问题讨论】:

  • uint8 的有效范围为0..255,因此数字22024283 不可能是uint8 类型。使用其他类型,例如int.
  • 谢谢。我认为字节是 Go 中 uint8 的别名?我打印出 reflect.TypeOf(itemArray[i])) 并为来自 GetTopStories() API 的那些长数字得到 uint8。
  • 是的,byteuint8 的别名。你的MakeHTTPRequest() 返回一个[]byte,所以索引它会产生byte 值。但这是 HTTP 响应,而不是单个项目 ID。

标签: string go hacker-news-api


【解决方案1】:

itemArray, _ := MakeHTTPRequest(url)

itemArray 必须像

一样解组
dat := make([]uint64, 0)
if err := json.Unmarshal(itemArray, &dat); err != nil {
        panic(err)
}

【讨论】:

  • 如果在解组之后,itemArray 的类型又是[]uint8,UnMarshal 在这种情况下有什么帮助?这是否意味着 itemArray(用于存储或传输的对象)的表示未转换为可执行对象的表示
  • itemArray 是这个 var 的错误名称。实际上是jsonResponse。您可以通过 fmt.Println(string(itemArray)) 进行检查。所以 json.Unmarshal 只是将 JSON 数据传输到 var dat。此数据导入的模式来自声明的类型 dat :​​= make([]uint64, 0)
  • 谢谢! fmt.Println(string(itemArray[i])) 按预期打印出一个项目 {"by":"pc","descendants":0,"id":91,"kids":[454519],"score":5,"time":1171908063,"title":"SunRocket cofounders up and leave","type":"story","url":"http://www.washingtonpost.com/wp-dyn/content/article/2007/02/11/AR2007021101198.html?nav=rss_technology"}。当我尝试使用json.Unmarshal(jsonItem, &amp;item) 将其转换为我定义的Item 结构时,我得到{ 0 0 [] 0 {0 0 &lt;nil&gt;} 你知道这是什么意思吗?
  • 你的 Item 结构只有私有字段,这就是原因。您需要使用大写作为字段名称的第一个字母
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-08
  • 2016-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多