Go package for interfacing with an OS hosts file
Find a file
Luther Monson 7d30adff3a
Merge pull request #57 from goodhosts/feature/vvv-improvements
Add batch check methods, safety features, and bug fixes
2026-01-05 20:39:56 -08:00
.github/workflows moving to mage 2023-10-03 11:06:38 -07:00
mage adding coverage mage helpers 2023-10-15 13:41:13 -07:00
.gitignore adding coverage mage helpers 2023-10-15 13:41:13 -07:00
build.go moving to mage 2023-10-03 11:06:38 -07:00
go.mod bumping testify to v1.8.4 2023-12-23 10:32:19 -07:00
go.sum bumping testify to v1.8.4 2023-12-23 10:32:19 -07:00
hosts.go Add batch check methods, safety features, and bug fixes 2025-12-30 17:14:39 +00:00
hosts_test.go Add batch check methods, safety features, and bug fixes 2025-12-30 17:14:39 +00:00
hostsline.go Add batch check methods, safety features, and bug fixes 2025-12-30 17:14:39 +00:00
hostsline_test.go Add batch check methods, safety features, and bug fixes 2025-12-30 17:14:39 +00:00
LICENSE adding apache license 2020-04-04 17:23:25 -07:00
lookup.go moved more functionality to lookups, refactored to preserve comments in the clean process 2023-10-03 02:28:53 -07:00
lookup_test.go moved more functionality to lookups, refactored to preserve comments in the clean process 2023-10-03 02:28:53 -07:00
magefile.go moving to mage 2023-10-03 11:06:38 -07:00
os.go moving to mage 2023-10-03 11:06:38 -07:00
os_windows.go moving to mage 2023-10-03 11:06:38 -07:00
README.md updating readme and comments 2023-10-16 00:55:04 -07:00
slice.go fixed reordering bug in removing duplicate hosts, fixed bug where if a hostsline was only a comment it never added the comment to the line, fixed remove logic to use addLine 2023-10-01 10:55:22 -07:00
slice_test.go add ip/host lookup hashmap to speedup and support multi location within the file 2021-12-02 10:35:06 -07:00

Go package for working with a system's hostsfile

codecov Go Reference

Reads the content of a file in the hosts format into go structs for easy manipulation in go programs. When all changes are complete you can Flush the hosts file back to disk to save your changes. Supports an indexing system on both ips and hosts for quick management of large hosts files.

Simple Usage

Simple usage reading in your system's hosts file and adding an entry for the ip 192.168.1.1 and the host my-hostname

package main

import (
	"log"
	
	"github.com/goodhosts/hostsfile"
)

func main() {
    hosts, err := hostsfile.NewHosts()
    if err != nil {
        log.Fatal(err.Error())
    }
    if err := hosts.Add("192.168.1.1", "my-hostname"); err != nil {
        log.Fatal(err.Error())
    }
    if err := hosts.Flush(); err != nil {
        log.Fatal(err.Error())
    }
}

Other Usage

Read in a hosts file from a custom location which is not the system default, this is useful for tests or systems with non-standard hosts file locations.

hosts, err := hostsfile.NewCustomHosts("./my-custom-hostsfile")

Use Add to put an ip and host combination in the hosts file

err := hosts.Add("192.168.1.1", "my-hostname")

Add is variadic and can take multiple hosts to add for the same ip

err := hosts.Add("192.168.1.1", "my-hostname", "another-hostname")

Use Remove to drop an ip and host combination from the hosts file

err := hosts.Remove("192.168.1.1", "my-hostname")

Remove is variadic and can take multiple hosts to remove from the same ip

err := hosts.Remove("192.168.1.1", "my-hostname", "another-hostname")

Flush the hosts file changes back to disk

err := hosts.Flush()