func Copy(src io.Reader, dst io.Writer) error { //... } import gocopy s := file.Open() err := gocopy.Copy(s, d) func Copy(src io.Reader, dst io.Writer) (statusCh, error) { //... go func() { statusCh }() } func Read() { select { case status := <-statusCh: } } type Stat struct { status string err error } func Copy(src io.Reader, dst io.Writer) chan Stat { //... statusCh := make(chan Stat{}) go func() { statusCh{ } } return statusCh } func Read() { select { case status := <-statusCh: } } import gocopy s := file.Open() err := gocopy.Copy(s, d) func Copy(src io.Reader, dst io.Writer) chan Stat { //... statusCh := make(chan Stat{}) buf := make([]byte, 1024) var total int64 go func() { for { n, err := src.Read(buf) if n > 0 { writen, err := dst.Write(buf[:n]) total += int64(writen) if err != nil { statusCh <- Stat{ status: "done with err", err: err, } return } } if err == io.EOF { statusCh <- Stat{ bCopy: total, status: "done", err: nil, } return } if err != nil { statusCh <- Stat{ status: "done with err", err: err, } return } } } return statusCh } func Read() { select { case status := <-statusCh: if status.err != nil { return } } } func Copy(src io.Reader, dst io.Writer) chan Stat { //... statusCh := make(chan Stat{}) buf := make([]byte, 1024) var total int64 go func() { for { select { case : n, err := src.Read(buf) if n > 0 { writen, err := dst.Write(buf[:n]) total += int64(writen) if err != nil { statusCh <- Stat{ status: "done with err", err: err, } return } } if err == io.EOF { statusCh <- Stat{ bCopy: total, status: "done", err: nil, } return } if err != nil { statusCh <- Stat{ status: "done with err", err: err, } return } default: statusCh <- Stat{ bCopy: total, status: "pending", err: nil, } } } } return statusCh } func Read() { select { case status := <-statusCh: if status.err != nil { return } } }
Golang
What are you looking for in a job, what do you want to do?
There is a function `unpredictableFunc()`, which needs to be wrapped so that it executes with a timeout (for example, 1 second), and its execution time is logged. // There is a function that runs indefinitely long and returns a number. // Its body cannot be changed (imagine it as a network request). func unpredictableFunc() int64 { rnd := rand.Int63n(5000) time.Sleep(time.Duration(rnd) * time.Millisecond) return rnd } // You need to modify the wrapper function to work with a specified timeout (for example, 1 second). // If the "long" function completes within this time - great, return the result. // If not - return an error. The result of the execution in this case is not important. // // Additionally, you need to measure how long this function took to execute (just log it). // The signature of the wrapper function can be changed. func predictableFunc() (int64, error) { start := time.Now() ch := make(chan int64, 1) errCh := make(chan error, 1) go func() { result := unpredictableFunc() ch <- result }() select { case res := <-ch: duration := time.Since(start) log.Printf("Function executed in %s", duration) return res, nil case <-time.After(1 * time.Second): duration := time.Since(start) log.Printf("Function timed out after %s", duration) return 0, errors.New("timeout") } }
Which team did you work in at the bank? What was the team responsible for?
How does SSL/TLS work? Describe the connection establishment process.
How are maps structured in Go?
What is a slice in Go?
Have you written operators for Kubernetes?
Как устроен context.WithTimeout внутри?
Have you considered splitting data into hot (last 3 months) and cold to reduce load instead of Elasticsearch?
What are transaction isolation levels? Which ones have you used?
//What will the code output? package main import "fmt" func main() { nums := make([]int, 1, 2) fmt.Println(nums) // appendSlice(nums, 1024) fmt.Println(nums) // mutateSlice(nums, 1, 512) fmt.Println(nums) // } func appendSlice(sl []int, val int) { sl = append(sl, val) } func mutateSlice(sl []int, idx, val int) { sl[idx] = val }
How to check CPU and memory load in Linux?
**Condition** Requests to multiple URLs should be executed in parallel. You need to output HTTP statuses and handle errors. ```go package main func main() { urls := []string{"[link]"} // Do something } ```
How do goroutines differ from OS threads? Why are goroutines needed?
How to find slow queries in PostgreSQL and how to optimize them?
How do you distinguish between Junior, Middle, and Senior developers? What criteria do you use to consider yourself a Senior?
Tell me about resp.Body.Close() — why is it needed and why did you set a buffer of 10?
What are indexes in databases? What types of indexes are there?
How do transactions work in PostgreSQL and MySQL?