一个api函数 要求返回datatime用GO封装函数接受

hellsam · 2018-01-05 22:18:55 · 1009 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2018-01-05 22:18:55 的主题,其中的信息可能已经有所发展或是发生改变。

这个是例子 大概演示下  对方API返回datatime 那么我想肯定用time.Time来接受吧 结果得到的都是0001-01-01 08:00:00
import(
    "time"
)

func GetApi()(create time.Time){
    result:= http.get("http://url/api=xxxxxxxxxx")  //这个返回interface{}
    return result
}

func main(){
    getdata:= GetApi()
    fmt.println(getdata..Local().Format("2006-01-02 15:04:05"))
}

最终的结果 返回 格式是正确了  返回都是 0001-01-01 08:00:00 , 难道我接受datatime的函数哪里写错了。??

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1009 次点击  
加入收藏 微博
10 回复  |  直到 2018-01-08 10:05:18
hellsam
hellsam · #1 · 7年之前

手写的代码 见谅 可能有些大小写没写正确。

hellsam
hellsam · #2 · 7年之前

额 有人回复? 没人我等会再问问

channel
channel · #3 · 7年之前

接口数据是字符串吧,先当字符串接收,然后转为 go 的 time.Time

hellsam
hellsam · #4 · 7年之前

@channel 我尝试这样写 返回的依然是 0001-01-01 08:00:00 ,不知道是什么原因 希望大牛帮看看 这样处理不对吗 API函数明确写明返回datetime类型

func main(){
    getdata:= GetApi()
    zh := getdata(time.Time)
    fmt.println(zh.Local().Format("2006-01-02 15:04:05"))
}
channel
channel · #5 · 7年之前

@hellsam 你所谓的api,返回的数据是什么格式,比如 json,那肯定也还是字符串,通过字符串解析为 Time 啊。time.Parse 相应方法。

hellsam
hellsam · #6 · 7年之前

@channel 我看过了返回 time.Time

channel
channel · #7 · 7年之前
hellsamhellsam #6 回复

@channel 我看过了返回 time.Time

那不明白你这 API 是啥玩意,再说了,都返回 time.Time ,那怎么可能会格式不对,那就是接口的问题了

leavesdrift
leavesdrift · #8 · 7年之前
channelchannel #5 回复

@hellsam 你所谓的api,返回的数据是什么格式,比如 json,那肯定也还是字符串,通过字符串解析为 Time 啊。time.Parse 相应方法。

不懂你的 zh := getdata(time.Time) 的作用,这句没啥必要,api返回的肯定是字符串,你就肯定只需要 time.Parse(s, format) 一下就能用,你这种要么 getdata 函数有问题,要么对方api有问题

leavesdrift
leavesdrift · #9 · 7年之前
hellsamhellsam #4 回复

@channel 我尝试这样写 返回的依然是 0001-01-01 08:00:00 ,不知道是什么原因 希望大牛帮看看 这样处理不对吗 API函数明确写明返回datetime类型 ~~~ func main(){ getdata:= GetApi() zh := getdata(time.Time) fmt.println(zh.Local().Format("2006-01-02 15:04:05")) } ~~~

··· func GetApi()(create time.Time){ result:= http.get("http://url/api=xxxxxxxxxx&#34:wink: //这个返回interface{} return result } ```

result怎么可能为time.Time类型,你要在这个函数里面就做好处理,而不是其他地方,这样写的唯一可能就是各个语言的数据结构已经都能直接在网络上传播了,而不需要字节流了。。。

juntaran
juntaran · #10 · 7年之前
func GetApi()(create time.Time){
    result:= http.get("http://url/api=xxxxxxxxxx")  //这个返回interface{}
    return result
}

你这个 result 不可能是 time 类型 他只能是一个 byte 或者 json 之类的

resp, _ := http.Get("url")
    body, _ := ioutil.ReadAll(resp.Body)
    log.Println(string(body))
    // 这里的 body 是一个 []byte 类型,你接下来只需要 []byte 转 int64 获得 timestamp
        // 再转时间就行了
    time.Unix(timestamp, 0).Format("2006-01-02 15:04:05")
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传