{"id":1607,"date":"2023-03-07T11:02:37","date_gmt":"2023-03-07T11:02:37","guid":{"rendered":"https:\/\/www.liutianfeng.com\/?p=1607"},"modified":"2023-03-07T11:03:36","modified_gmt":"2023-03-07T11:03:36","slug":"%e6%96%87%e6%9c%ac%e6%89%93%e5%8d%b0%e8%a1%a8%e6%a0%bc%ef%bc%9atext-tabwriter","status":"publish","type":"post","link":"https:\/\/www.liutianfeng.com\/?p=1607","title":{"rendered":"\u6587\u672c\u6253\u5370\u8868\u683c\uff1atext\/tabwriter"},"content":{"rendered":"\n<p><\/p>\n\n\n<p>\u00a0<\/p>\n<p>\u793a\u4f8b<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/ Copyright \u00a9 2016 Alan A. A. Donovan &amp; Brian W. Kernighan.\n\/\/ License: https:\/\/creativecommons.org\/licenses\/by-nc-sa\/4.0\/\n\n\/\/ See page 187.\n\n\/\/ Sorting sorts a music playlist into a variety of orders.\npackage main\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\t\"sort\"\n\t\"text\/tabwriter\"\n\t\"time\"\n)\n\n\/\/!+main\ntype Track struct {\n\tTitle  string\n\tArtist string\n\tAlbum  string\n\tYear   int\n\tLength time.Duration\n}\n\nvar tracks = []*Track{\n\t{\"Go\", \"Delilah\", \"From the Roots Up\", 2012, length(\"3m38s\")},\n\t{\"Go\", \"Moby\", \"Moby\", 1992, length(\"3m37s\")},\n\t{\"Go Ahead\", \"Alicia Keys\", \"As I Am\", 2007, length(\"4m36s\")},\n\t{\"Ready 2 Go\", \"Martin Solveig\", \"Smash\", 2011, length(\"4m24s\")},\n}\n\nfunc length(s string) time.Duration {\n\td, err := time.ParseDuration(s)\n\tif err != nil {\n\t\tpanic(s)\n\t}\n\treturn d\n}\n\n\/\/!-main\n\n\/\/!+printTracks\nfunc printTracks(tracks []*Track) {\n\tconst format = \"%v\\t%v\\t%v\\t%v\\t%v\\t\\n\"\n\ttw := new(tabwriter.Writer).Init(os.Stdout, 0, 8, 2, ' ', 0)\n\tfmt.Fprintf(tw, format, \"Title\", \"Artist\", \"Album\", \"Year\", \"Length\")\n\tfmt.Fprintf(tw, format, \"-----\", \"------\", \"-----\", \"----\", \"------\")\n\tfor _, t := range tracks {\n\t\tfmt.Fprintf(tw, format, t.Title, t.Artist, t.Album, t.Year, t.Length)\n\t}\n\ttw.Flush() \/\/ calculate column widths and print table\n}\n\n\/\/!-printTracks\n\n\/\/!+artistcode\ntype byArtist []*Track\n\nfunc (x byArtist) Len() int           { return len(x) }\nfunc (x byArtist) Less(i, j int) bool { return x[i].Artist &lt; x[j].Artist }\nfunc (x byArtist) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }\n\n\/\/!-artistcode\n\n\/\/!+yearcode\ntype byYear []*Track\n\nfunc (x byYear) Len() int           { return len(x) }\nfunc (x byYear) Less(i, j int) bool { return x[i].Year &lt; x[j].Year }\nfunc (x byYear) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }\n\n\/\/!-yearcode\n\nfunc main() {\n\tfmt.Println(\"byArtist:\")\n\tsort.Sort(byArtist(tracks))\n\tprintTracks(tracks)\n\n\tfmt.Println(\"\\nReverse(byArtist):\")\n\tsort.Sort(sort.Reverse(byArtist(tracks)))\n\tprintTracks(tracks)\n\n\tfmt.Println(\"\\nbyYear:\")\n\tsort.Sort(byYear(tracks))\n\tprintTracks(tracks)\n\n\tfmt.Println(\"\\nCustom:\")\n\t\/\/!+customcall\n\tsort.Sort(customSort{tracks, func(x, y *Track) bool {\n\t\tif x.Title != y.Title {\n\t\t\treturn x.Title &lt; y.Title\n\t\t}\n\t\tif x.Year != y.Year {\n\t\t\treturn x.Year &lt; y.Year\n\t\t}\n\t\tif x.Length != y.Length {\n\t\t\treturn x.Length &lt; y.Length\n\t\t}\n\t\treturn false\n\t}})\n\t\/\/!-customcall\n\tprintTracks(tracks)\n}\n\n\/*\n\/\/!+artistoutput\nTitle       Artist          Album              Year  Length\n-----       ------          -----              ----  ------\nGo Ahead    Alicia Keys     As I Am            2007  4m36s\nGo          Delilah         From the Roots Up  2012  3m38s\nReady 2 Go  Martin Solveig  Smash              2011  4m24s\nGo          Moby            Moby               1992  3m37s\n\/\/!-artistoutput\n\n\/\/!+artistrevoutput\nTitle       Artist          Album              Year  Length\n-----       ------          -----              ----  ------\nGo          Moby            Moby               1992  3m37s\nReady 2 Go  Martin Solveig  Smash              2011  4m24s\nGo          Delilah         From the Roots Up  2012  3m38s\nGo Ahead    Alicia Keys     As I Am            2007  4m36s\n\/\/!-artistrevoutput\n\n\/\/!+yearoutput\nTitle       Artist          Album              Year  Length\n-----       ------          -----              ----  ------\nGo          Moby            Moby               1992  3m37s\nGo Ahead    Alicia Keys     As I Am            2007  4m36s\nReady 2 Go  Martin Solveig  Smash              2011  4m24s\nGo          Delilah         From the Roots Up  2012  3m38s\n\/\/!-yearoutput\n\n\/\/!+customout\nTitle       Artist          Album              Year  Length\n-----       ------          -----              ----  ------\nGo          Moby            Moby               1992  3m37s\nGo          Delilah         From the Roots Up  2012  3m38s\nGo Ahead    Alicia Keys     As I Am            2007  4m36s\nReady 2 Go  Martin Solveig  Smash              2011  4m24s\n\/\/!-customout\n*\/\n\n\/\/!+customcode\ntype customSort struct {\n\tt    []*Track\n\tless func(x, y *Track) bool\n}\n\nfunc (x customSort) Len() int           { return len(x.t) }\nfunc (x customSort) Less(i, j int) bool { return x.less(x.t[i], x.t[j]) }\nfunc (x customSort) Swap(i, j int)      { x.t[i], x.t[j] = x.t[j], x.t[i] }\n\n\/\/!-customcode\n\nfunc init() {\n\t\/\/!+ints\n\tvalues := []int{3, 1, 4, 1}\n\tfmt.Println(sort.IntsAreSorted(values)) \/\/ \"false\"\n\tsort.Ints(values)\n\tfmt.Println(values)                     \/\/ \"[1 1 3 4]\"\n\tfmt.Println(sort.IntsAreSorted(values)) \/\/ \"true\"\n\tsort.Sort(sort.Reverse(sort.IntSlice(values)))\n\tfmt.Println(values)                     \/\/ \"[4 3 1 1]\"\n\tfmt.Println(sort.IntsAreSorted(values)) \/\/ \"false\"\n\t\/\/!-ints\n}\n<\/code><\/pre>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p><p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"https:\/\/www.liutianfeng.com\">liutianfeng.com<\/a> &raquo; <a href=\"https:\/\/www.liutianfeng.com\/?p=1607\">\u6587\u672c\u6253\u5370\u8868\u683c\uff1atext\/tabwriter<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u793a\u4f8b \/\/ Copyright \u00a9 2016 Alan A. A. Donovan &amp; Brian [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[74],"tags":[],"_links":{"self":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/1607"}],"collection":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1607"}],"version-history":[{"count":2,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/1607\/revisions"}],"predecessor-version":[{"id":1609,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/1607\/revisions\/1609"}],"wp:attachment":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}