- 4
You are making a file downloader. To make the downloads faster, you decide to use concurrency. Each file download will run as a
Can anyone give me some hints on this one pls
5 ответов
+ 9
package main
import "fmt"
//define the download() function
func download(s int, c chan int) {
var sum int
sum = 0
for i:=0; i<=s; i++ {
sum += i
}
c<-sum
}
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
ch3 := make(chan int)
var s1, s2, s3 int
fmt.Scanln(&s1)
fmt.Scanln(&s2)
fmt.Scanln(&s3)
go download(s1, ch1)
go download(s2, ch2)
go download(s3, ch3)
//output the sum of all results
fmt.Println(<-ch2 + <-ch1 + <-ch3)
}
+ 1
package main
import "fmt"
func download(s int, c chan int) {
var sum int
sum = 0
for i:=0; i<=s; i++ {
sum += i
}
c<-sum
}
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
ch3 := make(chan int)
var s1, s2, s3 int
fmt.Scanln(&s1)
fmt.Scanln(&s2)
fmt.Scanln(&s3)
go download(s1, ch1)
go download(s2, ch2)
go download(s3, ch3)
fmt.Println(<-ch2 + <-ch1 + <-ch3)
}
0
1. create a function with size and channel parameters
2. store sum of size to channel
3. sum all ch with
fmt.Println(<-ch1+<-ch2+<-ch3)
0
answer:
package main
import "fmt"
//define the download() function
func download(s int, c chan int) {
var sum int
sum = 0
for i:=0; i<=s; i++ {
sum += i
}
c<-sum
}
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
ch3 := make(chan int)
var s1, s2, s3 int
fmt.Scanln(&s1)
fmt.Scanln(&s2)
fmt.Scanln(&s3)
go download(s1, ch1)
go download(s2, ch2)
go download(s3, ch3)
//output the sum of all results
fmt.Println(<-ch2 + <-ch1 + <-ch3)
}
0
I really need this app but its so slow ?