Перейти к содержимому

InDrive - 1

func main() {
var strData string
for i := 0; i < 10; i++ {
strData += fmt.Sprintf("%d ", i)
}
fmt.Println(strData)
}
func main() {
defer fmt.Println(1)
time.Sleep(1 * time.Second)
go fmt.Println(2)
go fmt.Println(3)
}
func main() {
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
time.Sleep(2950 * time.Millisecond)
doDbRequest(ctx)
}
func doDbRequest(ctx context.Context) {
newCtx, _ := context.WithTimeout(ctx, 10*time.Second)
timer := time.NewTimer(1 * time.Second)
select {
case <-newCtx.Done():
fmt.Println("Timeout ")
case <-timer.C:
fmt.Println("Request Done ")
}
}
var counter = 0
func main() {
for i := 0; i < 10; i++ {
go func() { counter++ }()
}
time.Sleep(2 * time.Second)
fmt.Println(counter)
}
  • There are three goroutines where run is blocked. How to make the steady function simultaneously unblock all three runs? go/concurrency
func runnerStart() {
go ready()
go ready()
go ready()
steady()
// ensure they start running after the call, simultaneously
}
func steady() {
// write
}
func ready() {
// wait for steady call
run()
}
  • Complete the code so that the task runs asynchronously with a limit on the number of simultaneous requests go/concurrency
func main() {
// Make this code asynchronous with controlled parallel request numbers
for _, val := range getData() {
if !checkDomain(val) {
fmt.Printf("%s is bad \n ", val)
}
}
}
func getData() []string {
return []string{"1.test ", "2.test " /*...*/, "1000.test "}
}
func checkDomain(host string) bool {
return true
}
func main() {
// a, b - sorted slices
var a = []int{1, 5, 6, 18, 99}
var b = []int{2, 4, 9, 11}
fmt.Println(mergeSorted(a, b))
}
func mergeSorted(a []int, b []int) {
// write
}
  • There is a method that iterates through all records from a table by increasing the offset. What problems do you see? sql/query-optimization
select*from news where category_id =1 limit 10 offset 100;
// building a weather app. Getting weather forecast by country.
// Data updates come infrequently relative to reads and irregularly.
// A 2-hour data lag is acceptable
// The DB works poorly and slowly, 700-800ms
type database interface {
get(ctx context.Context, key string) (string, error)
save(ctx context.Context, key, value string) error
}
type Repository struct {
db database
}
func (r *Repository) Get(ctx context.Context, key string) (string, error) {
return r.db.get(ctx, key)
}
func (r *Repository) Save(ctx context.Context, key, value string) error {
return r.db.save(ctx, key, value)
}