推断方法的最佳实践是什么? [英] What's the best practice to infer the methods?

查看:154
本文介绍了推断方法的最佳实践是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义时间格式,我用来正确编码/解码json。但是,每当我需要做一个时间计算,我需要做一个演员。这是正确的方法吗?感觉有点丑,以保持铸造。例如,当我需要更新我需要投射的价值两次(一次一次,一次对我的类型)

  type Mytime time.Time 
var t Mytime
t = Mytime(time.Now())
//向我键入的时间添加一小时
t = Mytime(time.Time t).Add(1 * time.Hour))


解决方案

假设您有键入Mytime time.Time 。如果改为嵌入它:

 类型MyTime struct {
time.Time
}

然后您可以:

  func(t MyTime)MarshalJSON()([] byte,error){
... whatever ...
}


并仍然访问 time.Time 的所有方法。
例如例如:

  t:= MyType {time.Now()} 
t.Time = t.Add time.Hour)






Fuller示例和非嵌入式自定义时间类型。注意,嵌入仍然不允许对期望 time.Time 值的事物进行透明的互操作。 (此处省略了这些类型的原因,例如添加 MarshalJSON 方法)。

  package main 

import(
fmt
time


类型YourTime时间。时间

类型MyTime struct {time.Time}

//一些随机函数,可能在第三方包中,
//处理time.Time值。
func fn(t time.Time){
fmt.Println(fn got:,t)
}

func fn2()time.Time {
return time.Unix(14e8,0)
}

func main(){
var t1 = YourTime(time.Now())
// t1 = t1.Add(time.Hour)//编译器错误
t1 = YourTime(time.Time(t1).Add(time.Hour))
fmt.Println(ugly t1: ,t1)
fmt.Println(nice t1:,time.Time(t1))
// fn(t1)//编译器错误
fn(time.Time ))

// t1 = fn2()//编译器错误
t1 = YourTime(fn2())

var t2 = MyTime {time.Now )}
// t2 = t2.Add(time.Hour)//编译器错误
t2.Time = t2.Add(time.Hour)
fmt.Println(t2: ,t2)
// fn(t2)//编译器错误
fn(t2.Time)

// t2 = fn2()//编译器错误
t2.Time = fn2()
}

Playground



输出:

  ugly t1:{63393494400 0 0x1c9340} 
nice t1:2009-11-11 00:00:00 +0000 UTC
fn get:2009-11-11 00:00:00 +0000 UTC
t2:2009-11-10 23:00:00 +0000 UTC
fn got:2009-11-10 23: 00:00 +0000 UTC


I have a custom time format which I use to properly encode/decode json. However whenever I need to do a time computation I need to do a cast. Is this the right way? It feels a bit ugly to keep casting. For example when I need to "update" the value I need to cast it twice ( once to time and once to my type)

type Mytime time.Time
var t Mytime
t = Mytime(time.Now())
// Add an hour to my typed time
t = Mytime(time.Time(t).Add(1 * time.Hour))

解决方案

Presumably you have type Mytime time.Time. If instead you embedded it:

type MyTime struct {
        time.Time
}

Then you could have:

func (t MyTime) MarshalJSON() ([]byte, error) {
        … whatever …
}

and still access all of time.Time's methods. E.g. something like:

        t := MyType{time.Now()}
        t.Time = t.Add(time.Hour)


Fuller example showing differences between embedded and non-embedded custom time types. Note that embedding still doesn't allow for transparent inter-use with things that expect a time.Time value. (The reason for making these types, e.g. to add a MarshalJSON method has been omitted here).

package main

import (
    "fmt"
    "time"
)

type YourTime time.Time

type MyTime struct{ time.Time }

// Some random functions, perhaps in a third party package,
// that deals with time.Time values.
func fn(t time.Time) {
    fmt.Println("fn got:", t)
}

func fn2() time.Time {
    return time.Unix(14e8, 0)
}

func main() {
    var t1 = YourTime(time.Now())
    //t1 = t1.Add(time.Hour)             // compiler error
    t1 = YourTime(time.Time(t1).Add(time.Hour))
    fmt.Println("ugly t1:", t1)
    fmt.Println("nice t1:", time.Time(t1))
    //fn(t1)                        // compiler error
    fn(time.Time(t1))

    //t1 = fn2()                    // compiler error
    t1 = YourTime(fn2())

    var t2 = MyTime{time.Now()}
    // t2 = t2.Add(time.Hour)       // compiler error
    t2.Time = t2.Add(time.Hour)
    fmt.Println("t2:", t2)
    //fn(t2)                        // compiler error
    fn(t2.Time)

    //t2 = fn2()                    // compiler error
    t2.Time = fn2()
}

Playground

Output:

ugly t1: {63393494400 0 0x1c9340}
nice t1: 2009-11-11 00:00:00 +0000 UTC
fn got: 2009-11-11 00:00:00 +0000 UTC
t2: 2009-11-10 23:00:00 +0000 UTC
fn got: 2009-11-10 23:00:00 +0000 UTC

这篇关于推断方法的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆