custom date types (time.Time)

polaris · · 810 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m creating some custom time.Time types. For example:</p> <pre><code>type DateType time.Time </code></pre> <p>I use this type in a struct:</p> <pre><code>type DateField struct { Value DateType } </code></pre> <p>When I attempt to create a variable of DateField, I&#39;m getting an error</p> <pre><code>cannot use time.Time literal (type time.Time) as type DateType in assignment </code></pre> <p>I&#39;m unable to see why this is failing. I can create custom types of ints and strings and do the same, if I wanted, but for time, it&#39;s just wrong.</p> <p>Edit: example code <a href="http://play.golang.org/p/3OjlbkHijw" rel="nofollow">http://play.golang.org/p/3OjlbkHijw</a></p> <hr/>**评论:**<br/><br/>TheMerovius: <pre><p>FYI: the reason it works with ints and strings is, that you aren&#39;t using ints and strings, but untyped integer and string constants. A literal 42 or &#34;foobar&#34; has no type, only when you assign it to a variable or use it in an expression does it get one and it&#39;s the type that&#39;s needed at that point (with a default of int/string). Effectively, that means if you do</p> <pre><code>type Foo int var foo Foo = 42 </code></pre> <p>the 42 will have type Foo. But a time.Time literal <em>is</em> typed, so you can only assign it to a variable/field of type time.Time.</p></pre>st1nc1ty: <pre><p>Good information. I was wondering why I was able to do it without anything yelling at me. Thanks Merovius</p></pre>kylewolfe: <pre><p>You need a type assertion: <a href="http://play.golang.org/p/Q_0CQX2BTr" rel="nofollow">http://play.golang.org/p/Q_0CQX2BTr</a></p></pre>dchapes: <pre><p>Nit-pick, <code>MyType(var1)</code> is a <a href="https://golang.org/ref/spec#Conversions" rel="nofollow">conversion</a>, not a <a href="https://golang.org/ref/spec#Type_assertions" rel="nofollow">type assertion</a>. A type assertion looks like <code>var2 := var1.(MyType)</code> (or <code>var2, ok := var1.(MyType)</code>). Conversions and type assertions are different things and shouldn&#39;t be confused with each other.</p></pre>st1nc1ty: <pre><p>Hmm... I don&#39;t think what I&#39;m doing is going to work then. Thank you for the help though.</p> <p>edit: I should say that this is what I was looking for but after seeing the value printed, it looks like this won&#39;t work with my current approach. time.Time has a proper zero datetime value.</p></pre>kylewolfe: <pre><p>I&#39;m not sure I follow, but don&#39;t forget that your custom DateType is still a time.Time, and behaves exactly the same (other than any additional methods you decide to tack on). You just need to assert back and fourth to get access to each set of methods. Maybe post a little more info about what your trying to solve for?</p></pre>JuggleTux: <pre><p>if you use <code>type DataType struct { time.Time }</code> you don&#34;t need to assert back and forward to get the different methods.</p> <p>if <code>DataType</code> and <code>time.Time</code> have a method e.g. <code>String()</code> with the same name it takes the method from <code>DataType</code> but you can still access the underlaying method like this <code>DataTypen.Time.String()</code></p> <p>you still need to assert it to store a <code>time.Time</code> in a <code>DataType</code> variable or to return a <code>time.Time</code> from <code>DataType</code></p> <p>found in: <a href="https://golang.org/doc/effective_go.html#embedding" rel="nofollow">https://golang.org/doc/effective_go.html#embedding</a></p></pre>st1nc1ty: <pre><p>Thanks! This is much closer to my original intent even though I&#39;ve gone another way I may circle back around and utilize it later on.</p></pre>st1nc1ty: <pre><p>Silly as it sounds I&#39;m creating a very simple ORM. Instead of using reflection to handle conversion between Golang struct &#34;models&#34; to a SQL type, I&#39;m creating my own types and &#34;Fields&#34; that go in the models. As of now, I&#39;ve gone back and decided to just set the value in DateField to time.Time like I had it originally. I may return back to custom field types and will utilize what you&#39;ve told me for the time types.</p></pre>kylewolfe: <pre><p>FYI in case you are unaware of it&#39;s existence: <a href="https://github.com/jinzhu/gorm" rel="nofollow">https://github.com/jinzhu/gorm</a></p> <p>Haven&#39;t used it myself yet, but I have a project coming up that i plan to try it on.</p></pre>st1nc1ty: <pre><p>Yep I know of it and have used it a bit. I&#39;m just writing mine to, well, just write it :D Nothing exotic as replacing it but I will show it off once I&#39;m done, I am proud of what I have so far.</p></pre>dchapes: <pre><p>It would appear you just tried some syntax and were then surprised it didn&#39;t do what you thought it would. You should read the <a href="https://golang.org/ref/spec" rel="nofollow">Go language spec</a> instead making unwarranted assumptions.</p> <p>I posted <a href="http://stackoverflow.com/a/29397985/55504" rel="nofollow">a Stackoverflow answer</a> that shows the syntax/usage differences between using a plain type definition (as you have) and using struct embedding [that answer is in the context of JSON (un)marshalling, but it&#39;s still relevant]. Depending on what you want one may be preferable over the other. And, as you&#39;ve discovered, for reasons given in the FAQ, Go doesn&#39;t allow automatic conversions between compatible types.</p></pre>st1nc1ty: <pre><p>Best way to learn in my opinion is do and figure it out as you go. I&#39;d been going around Go docs trying to figure it out on my own but when I couldn&#39;t find the right resource (via googling furiously :D) I decided to pose the question here. I&#39;ve learned something and now thanks to you have another resource to check out.</p></pre>

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

810 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传