timer := time.NewTimer(1 * time.Second)
<-timer.C
func worker(id int, jobs <-chan int, result chan<- int) {
for j := range jobs{
fmt.Printf("id:%d, jobs:%d \n", id, j)
time.Sleep(1 * time.Second)
result <- j
}
}
func main() {
jobs := make(chan int, 100)
result := make(chan int, 100)
for i := 1; i <= 3; i++ {
go worker(i, jobs, result)
}
for i := 1; i <= 9; i++ {
jobs <- i
}
close(jobs)
for i := 0; i < 9; i++ {
<-result
}
}
func main() {
var input string
fmt.Scanln(&input)
fmt.Println("done ", input)
}
func main() {
c1 := make(chan int)
go func() {
time.Sleep(2 * time.Second)
c1 <- 1
}()
select {
case msg := <-c1:
fmt.Println("get", msg)
case <-time.After(1 * time.Second):
fmt.Println("time out")
}
func main() {
messages := make(chan string, 1)
select {
case msg := <-messages:
fmt.Println("received messages", msg)
default:
fmt.Println("no received messages")
}
msg := "hi"
select {
case messages <- msg:
fmt.Println("sent messages", msg)
default:
fmt.Println("no messages sent")
}
select {
case msg := <-messages:
fmt.Println("received messages", msg)
default:
fmt.Println("no received messages")
}
}
requests := make(chan int, 5)
for i := 0; i < 5; i++ {
requests <- i
}
close(requests)
limiter := time.Tick(1 * time.Second)
for r := range requests {
<- limiter
fmt.Println(time.Now(), " ", r)
}
requests2 := make(chan int, 10)
for i := 0; i < 10; i++ {
requests2 <- i
}
close(requests2)
timeLimiter := make(chan time.Time, 3)
for i := 0; i < 3; i++ {
timeLimiter <- time.Now()
}
go func() {
for t := range time.Tick(1 * time.Second) {
timeLimiter <- t
}
}()
for r := range requests2 {
<- timeLimiter
fmt.Println(time.Now(), " ", r)
}
type argError struct {
arg int
prop string
}
func (e *argError)Error()string {
return fmt.Sprintf("hey I'm bad", e.arg, e.prop)
}
func f2(arg int)(int, error) {
if arg == 41{
return -1, &argError{arg, "f2 cant't work"}
}
return arg, nil
}
func main() {
fmt.Println(f2(1))
fmt.Println(f2(41))
}
func main() {
defer fmt.Println("end")
os.Exit(3)
}
s := "http://aaa:[email protected]:21/file?a=b#q"
u, err := url.Parse(s)
if err != nil {
panic(err)
}
fmt.Println(u.Scheme)
fmt.Println(u.User)
fmt.Println(u.User.Username())
p, _ := u.User.Password()
fmt.Println(p)
fmt.Println(u.Host)
h := strings.Split(u.Host, ":")
fmt.Println(h[0])
fmt.Println(h[1])
fmt.Println(u.Path)
fmt.Println(u.Fragment)
fmt.Println(u.RawQuery)
m, _ := url.ParseQuery(u.RawQuery)
fmt.Println(m)
fmt.Println(m["a"][0])
func check(e error) {
if e != nil {
panic(e)
}
}
d1 := []byte("hello go\n")
err := ioutil.WriteFile("/test.txt", d1, 0644)
check(err)
f, err := os.Create("/test2.txt")
defer f.Close()
check(err)
d2 := []byte{115, 111, 109, 101, 10}
n2, err := f.Write(d2)
check(err)
fmt.Println("wrote %d bytes \n", n2)
n3, err := f.WriteString("wrote string \nhey\n")
check(err)
fmt.Println("wrote %d bytes \n", n3)
f.Sync()
w := bufio.NewWriter(f)
n4, err := w.WriteString("buffered \n")
fmt.Println("wrote %d bytes \n", n4)
w.Flush()
func main() {
dat, err := ioutil.ReadFile("/test.txt")
Check(err)
fmt.Println(string(dat))
f, err := os.Open("/test.txt")
defer f.Close()
Check(err)
b1 := make([]byte, 12)
n1,err := f.Read(b1)
Check(err)
fmt.Printf("%d bytes: %s \n",n1, string(b1))
o2,err := f.Seek(6, 0)
Check(err)
b2 := make([]byte, 6)
n2,err := f.Read(b2)
Check(err)
fmt.Printf("%d bytes @ %d: %s \n", n2, o2, string(b2))
o3, err := f.Seek(6, 0)
Check(err)
b3 := make([]byte, 3)
n3,err := io.ReadAtLeast(f, b3, 3)
Check(err)
fmt.Printf("%d bytes @ %d: %s \n", n3, o3, string(b3))
_, err = f.Seek(0, 0)
Check(err)
r4 := bufio.NewReader(f)
b4, err := r4.Peek(6)
Check(err)
fmt.Printf("6 bytes: %s \n", string(b4))
}
func main() {
strs := []string{"b", "c", "a"}
sort.Strings(strs)
fmt.Println("strings sort: ", strs)
ints := []int{3,1,2}
sort.Ints(ints)
fmt.Println("ints sort: ", ints)
areIntsSort := sort.IntsAreSorted(ints)
fmt.Println("are ints sort: ", areIntsSort)
}
type ByLengthSort []string
func (s ByLengthSort)Len() int {
return len(s)
}
func (s ByLengthSort)Swap(i int, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ByLengthSort)Less(i int, j int)bool {
return len(s[i]) < len(s[j])
}
func main() {
strs := []string{"aaa", "bb", "dddd", "c"}
sort.Sort(ByLengthSort(strs))
fmt.Println(strs)
}
f, _ := strconv.ParseFloat("1.234", 64)
i, _ := strconv.ParseInt("123", 0, 64)
d, _ := strconv.ParseInt("0x1c8", 0, 64)
k, _ := strconv.Atoi("135")
func main() {
fmt.Println(time.Now())
it := time.Date(
2012,12,13,0,0,0,0, time.UTC)
fmt.Println(it)
fmt.Println(it.Weekday())
fmt.Println(it.Before(time.Now()))
diff := time.Now().Sub(it)
fmt.Println(diff)
fmt.Println(diff.Hours())
fmt.Println(diff.Nanoseconds())
fmt.Println(it.Add(diff))
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
ucl := strings.ToUpper(scanner.Text())
if ucl == "#"{
return
}
fmt.Println(ucl)
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "error", err)
os.Exit(1)
}
}
s3 := rand.NewSource(int64(time.Now().Second()))
r3 := rand.New(s3)
for i := 0; i < 10; i++ {
fmt.Print(r3.Intn(100), ",")
}