Type query for interface in go: Comma-ok assertion and switch test

Source: Internet
Author: User

Code_026_go_antic_package Project Main.go<pre name= "code" class= "plain" >package mainimport ("Container/list" "FMT" "Math/rand"//NOTE 2: Random number of packages "sync"//NOTE 1: Asynchronous task Package "time") type INFO struct {lock sync. Mutex//NOTE 1: Asynchronous lock Name string time Int64}var List *list. List = list. New ()//NOTE 3: Initialize the list variable func main () {var info info go func () {for i: = 0; i < 5; i++ {time. Sleep (time. Duration (1E9 * Int64 (RAND). INTN (5)))//NOTE 2: Random number Rand. INTN (5) <---> 1e9 is scientific notation, 1 * 10 of 9 Info.lock.Lock ()//NOTE 1: Locked info.name = fmt. Sprint ("Name", I)///NOTE: Sprint formats its parameters in the default format, concatenates all output and returns a string. If two adjacent parameters are not strings, a space is added between their outputs Info.time = time. Now (). Unix () + 3 Info.lock.Unlock ()//NOTE 1: Unlock list.pushback (Info)//comment 3:list expression call}} () go Getgoods () Select {}}func getgoods () {f or {time. Sleep (1e8) for List.len () > 0 {//Memo 3:list object using N, T: = List.remove (List.front ()). ( INFO). Name ()//Memo 3:list the use and value of the object. The magic of (type) Now: = time. Now (). Unix ()//Note 4: Gets the current date after the converted timestamp if T-now <= 0 {fmt. Println (N, T, now) continue} time. Sleep (time. Duration ((t-now) * 1e9)) fmt. Println (N, T, Now)}}}func (i INFO) name (string, Int64) {return i.name, i.time}

Type queries for interfaces in go: COMMA-OK assertions and switch tests

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.