github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/util/net/net_interfaces.go (about)

     1  package net
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  )
     7  
     8  // LoopbackInterfaceName search for the name of a loopback interface in the list
     9  // of the system's network interfaces and returns the first one found.
    10  func LoopbackInterfaceName() (string, error) {
    11  	is, err := net.Interfaces()
    12  	if err != nil {
    13  		return "", fmt.Errorf("can't retrieve loopback interface name: %s", err)
    14  	}
    15  
    16  	for _, i := range is {
    17  		if i.Flags&net.FlagLoopback != 0 {
    18  			return i.Name, nil
    19  		}
    20  	}
    21  
    22  	return "", fmt.Errorf("can't retrieve loopback interface name")
    23  }