github.com/DARA-Project/GoDist-Scheduler@v0.0.0-20201030134746-668de4acea0d/examples/MultiFileRead/file_read.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"time"
     7  )
     8  
     9  // Opens, reads, and closes "file2.txt"
    10  // and then goes to sleep
    11  func bar(c chan int) {
    12  	f, err := os.Open("file2.txt")
    13  	if err != nil {
    14  		log.Fatal(err)
    15  	}
    16  
    17  	b1 := make([]byte, 20)
    18  	_, err = f.Read(b1)
    19  	if err != nil {
    20  		log.Fatal(err)
    21  	}
    22  	f.Close()
    23  	time.Sleep(time.Millisecond)
    24  	c <- 1
    25  
    26  }
    27  
    28  // Opens, reads, and closes "file.txt"
    29  // and then goes to sleep
    30  func foo(c chan int) {
    31  	f, err := os.Open("file.txt")
    32  	if err != nil {
    33  		log.Fatal(err)
    34  	}
    35  
    36  	b1 := make([]byte, 20)
    37  	_, err = f.Read(b1)
    38  	if err != nil {
    39  		log.Fatal(err)
    40  	}
    41  	f.Close()
    42  	time.Sleep(time.Millisecond)
    43  	c <- 1
    44  }
    45  
    46  func main() {
    47  
    48  	c := make(chan int)
    49  	c2 := make(chan int)
    50  	go foo(c)
    51  	go bar(c2)
    52  	x := <-c
    53  	y := <-c2
    54  	log.Println("Got x", x)
    55  	log.Println("Got y", y)
    56  
    57  }