Home > Article > Backend Development > How to set a static IP address in golang
Go language (golang) is an open source programming language that can run on different operating systems. When doing network programming, one of the essential steps is to set a static IP address, which can improve the stability and security of network communication. This article will introduce how to set a static IP address in golang.
When using golang in network programming, you need to get the network card interface name. First, you need to import the net package and use the net.Interfaces() function to obtain device information. Then, traverse the device and get the name of the device, as shown below:
package main import ( "fmt" "net" ) func main() { ifs, err := net.Interfaces() if err != nil { fmt.Println("Error: ", err) } for _, i := range ifs { fmt.Println(i.Name) } }
After executing the above code, all available network card interface names on the computer will be output, for example:
en0 en1 lo0
To set a static IP address in golang, you need to use the netlink library in the net package. This library provides a method for setting the IP address of the network card. You need to import the netlink library and then use the SetLinkIP function to set the IP address. For example, we can set the IP address to 192.168.0.100, the mask to 255.255.255.0, and the gateway to 192.168.0.1. The code is as follows:
package main import ( "fmt" "net" "github.com/vishvananda/netlink" ) func main() { link, err := netlink.LinkByName("en0") if err != nil { fmt.Println("Error: ", err) } addr, err := netlink.ParseAddr("192.168.0.100/24") if err != nil { fmt.Println("Error: ", err) } netlink.AddrReplace(link, addr) route := netlink.Route{ LinkIndex: link.Attrs().Index, Gw: net.ParseIP("192.168.0.1"), } netlink.RouteReplace(&route) }
After executing the above code, static settings will be set for the network card en0 IP address.
Use the golang program to check whether the IP address is set successfully. You can view the IP address of the device by executing the system command ifconfig. The following code demonstrates how to execute ifconfig to obtain the IP address of the specified network card:
package main import ( "fmt" "os/exec" "strings" ) func main() { cmd := exec.Command("ifconfig", "en0") data, err := cmd.Output() if err != nil { fmt.Println("Error: ", err) } output := string(data) scanner := bufio.NewScanner(strings.NewReader(output)) for scanner.Scan() { line := scanner.Text() if strings.Contains(line, "inet ") { fields := strings.Fields(line) fmt.Println("IP address: ", fields[1]) } } }
After executing the above code, the IP address of the computer's network card en0 will be output.
Summary: By using the net package and netlink library in golang, we can easily set the static IP address of the network card, improving the reliability and security of the application.
The above is the detailed content of How to set a static IP address in golang. For more information, please follow other related articles on the PHP Chinese website!