Advantages and disadvantages of Golang for DevOps Development (six): Time package and method overloading

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. [] (https://raw.githubusercontent.com/studygolang/gctt-images/master/go_devops/ Golang-pros-cons-4-time-package-method-overloading.png) The long awaited Golang of the pros and cons of DevOps development is finally back! In this article, we discuss the time package in Golang, and why the go language does not use method overloading. If you do not read [recent] (https://studygolang.com/articles/12614) About "interface implementation and public/private naming method" (the original description is wrong, this link should be "speed vs. lack of generics"), please read carefully , you can also [subscribe to our Blog UPDATE] (http://eepurl.com/cOHJ3f), you will be notified when a series of articles are published. -[Golang's pros and cons of DevOps development (one of six): Goroutines, Channels, panics, and Errors] (https://studygolang.com/articles/11983)-[ Advantages and disadvantages of Golang for DevOps Development (part Six): Automation and public/private implementation of interface implementations] (https://studygolang.com/articles/12608)-[Golang to DevOps Pros and cons of development (six part Three): Speed vs. lack of generics] (https://studygolang.com/articles/12614)-[Golang's pros and cons of DevOps development (six): Time package and method overloading] ( https://studygolang.com/articles/12615)-[Golang's pros and cons of DevOps development (six): Cross-platform compilation, Windows,signals,docs and compilers] (https:// studygolang.com/articles/12616)-The pros and cons of Golang for DevOps development (six part Six): Version control of Defer directives and package dependencies # # Golang: The people who use programming for time packages know to deal with date andThe danger of time. Time is a common occurrence in our daily life, and from a computer point of view it is a nightmare. Google has this opportunity to make it easier to deal with date, * they succeeded *! I divided the [time package] (https://golang.org/pkg/time/) instruction into three parts: (1) basic content, (2) Timer timer, (3) Date parsing. # # 1. Time Package basic content you might think that every language has a standard, easy-to-use built-in library that handles time operations, not really. NPM has more than [more than 8,000 time-related packages] (https://www.npmjs.com/search?q=time&page=1&ranking=quality) because JavaScript's Date The bag can't be used. Java8 eventually alleviated this problem with the java.time.Instant and Java.time.chrono packages, but was still writing [tutorial] (https://www.tutorialspoint.com/java8/java8_ datetime_api.htm), studying the various classes and methods of working with time in Java. Instead, Golang's [Time package] (https://golang.org/pkg/time/) can be summed up in a single sentence: You just have to refer to a package and you can achieve it. Gets the current time: ' Times. Now () ' Get a moment in the future: ' Time. Now (). ADD (5*time. Minute) ' Get elapsed Time (duration): ' Time. Since (processingstarted) ' Easy to compare duration: ' If frequency < time. Hour {' Get the date of the month, give up [calendar] (http://https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html)! ' Time. Now (). Day () ' is Enough for # # # 2. Timers Timer This time package another big plus is the convenience of using a timer timer. The application of DevOps is clear: we often need to schedule some tasks to be performed in the future, some repetitive basic operations, or just sleep for a while. With the co-locating timer in the time package, you can easilyNow sleep a thread, such as ' Gotime '. Sleep (2*time. Minute) ' or execute a function ' ' gotime at some point in the future. Afterfunc (5*time. Second, func () {fmt. Println ("Hello in the Future")}) "or repeat a task" gotick: = time. Newticker (1*time. Minute) Select {Case <-tick. C:foo ()} "(these operations) do not need to convert time to second (or Millsecond again?) ), (and not) add 2 dependent libraries (and not) to introduce 10 packages. # # 3. Date parsing doesn't say our most common and annoying topic: turning a string into date, the discussion about time is incomplete. This is supposed to be a primary problem, but it's not easy. The date format has [many standards] (https://en.wikipedia.org/wiki/ISO_8601), [There is no thread-safe implementation within the programming language] (https://stackoverflow.com/questions /6840803/why-is-javas-simpledateformat-not-thread-safe) to parse the date and [timezone] (https://en.wikipedia.org/wiki/List_ Of_tz_database_time_zones) is to ensure that not all places (at the same time) are 5 o'clock. This is the important thing that Golang realizes: what other people do is wrong. Instead of building a solution to handle thousands of date formats, Google created a system that uses pattern-based design and always references the same reference time: Mon Jan 15:04:05 MST 2006. The elements that make up this single reference time can be re-assembled into any format you might encounter. You can find the exact, complete description here (the original text does not have a link, suspect [here] (https://golang.org/pkg/time/#example_Time_Format)). Of course, Golang's date parsing is not perfect, is it? Well, of course. You should at least consider a problem: timezone, or you cannot re-parse date. Besides, it's impossible to arrange a meeting, orBecause Golang in the timezone, let similar "2017/10/03 4:07:22 america/new_york" Such resolution is more difficult. "America/new_york" These bits (parse up) is more troublesome. There are some [workarounds] (https://stackoverflow.com/a/25368749), but Golang in parsing timezone this part needs to be improved. # # # Golang: function Overloading I think about the computer science class when the first feeling is "too cool!" "But there is no such thing as" cool "in Golang. The following comes from Golang faq:> Why does Go not support method and operator overloading? > The method scheduling process is simplified if it is not used for type matching. Experience with other languages tells us that some functions with the same name but different signatures are sometimes useful, but in practice they can be misleading and not robust enough. > Go type System The primary use of the simplified decision is only through the function name matching, and requires the type of consistency. In the case of overloading of operators, it seems to be more convenient than absolute requirements (type consistency). However, it would be easier without overloading. Of course, no method overloading is really easier. But this will also force you to come up with a unique function name in case the same name functions, which increases the amount of your code. If you just want to copy and paste the code, "solve it later", remember that the shared function works in private or public functions. There are several instances of the time package mentioned later, and we specifically want to replace the last optional parameter with a default value. "' Gofunc parse (layout, value string) (time, error) {return parse (layout, value, UTC, Local)}func parseinlocation (layout, Value string, loc *location) (time, error) {return parse (layout, value, loc, loc)}func parse (layout, value string, Defaul Tlocation, local *location) (time, error) {...} "[[Source Code Address]] (https://golang.org/src/time/format.go?s=23626:23672#L762) This means that there is no method overload that does not prevent the use of Golang. In a few IIn an instance where I wanted to use (reload), I ended up using only a shared private function, and several other functions with different names, or deleted the reason I said I needed to overload at first. Every other week or so, we will release a new guide, as in this article in the "Golang to the pros and cons of DevOps development" in part six. Filed under: #5: Cross-platform compilation, Windows, signals, Docs and compilers. Now [subscribe to our Blog] (http://eepurl.com/cOHJ3f) So you don't miss out on our post, if you think your developer friends can benefit from understanding the pros and cons of Golang, please share this blog post with them, especially about DEVOPS Life-cycle. Thank you for reading and sharing.

Via:https://blog.bluematador.com/golang-pros-cons-part-4-time-package-method-overloading

Author: Matthew barlocker Translator: arisaries proofreading: polaris1119

This article by GCTT original compilation, go language Chinese network honor launches

This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove

305 Reads
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.