Home > Backend Development > Golang > How to set a static IP address in golang

How to set a static IP address in golang

PHPz
Release: 2023-03-30 09:47:11
Original
1418 people have browsed it

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.

  1. Get the network card interface name

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)
    }
}
Copy after login

After executing the above code, all available network card interface names on the computer will be output, for example:

en0
en1
lo0
Copy after login
  1. Settings Static IP address

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)
}
Copy after login

After executing the above code, static settings will be set for the network card en0 IP address.

  1. Check whether the IP address is set successfully

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])
        }
    }
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template