Sobes.tech
Senior

func main() { ctx := context.Background() urls := []string{ "vk.ru", "ozon.ru", "pi.zone.ru", "lamoda.ru", "adidas.com", } pingMap, err := collectRTT(ctx, urls) if err != nil { fmt.Println("ERR:", err) return } data, _ := json.Marshal(pingMap) fmt.Println("RTT:", string(data)) } func collectRTT(ctx context.Context, urls []string) (map[string]time.Duration, error) { pingMap := make(map[string]time.Duration, len(urls)) var mu sync.Mutex var wg sync.WaitGroup errCh := make(chan error, 1) for _, url := range urls { wg.Add(1) go func(u string) { // ... (rest of the function implementation) }(url) } // ... (rest of the function implementation) } Ошибка: # command-line-arguments 2486763/main.go:34:9: undefined: sync 2486763/main.go:35:9: undefined: sync 2486763/main.go:45:32: undefined: fmt.Errorf

sobes.tech AI

Answer from AI

The errors undefined: sync and undefined: fmt.Errorf occur due to missing imports of the necessary packages in your Go file. In Go, to use the sync and fmt packages, you need to explicitly import them.

Add the following import block at the beginning of your file:

import (
    "context"
    "encoding/json"
    "fmt"
    "sync"
    "time"
)

Without this, the compiler will not be able to find the definitions of sync.Mutex, sync.WaitGroup, and fmt.Errorf.

Also, ensure that you are using the correct function and variable names, and that all your code is within the main package or another appropriate package.

Here's a minimal import example for your code:

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "sync"
    "time"
)

func main() {
    // your code
}

// other functions

This will resolve the undefined issue.

func main() { ctx := context.Background() urls :=… - sobes.tech