This library is now deprecated. See the goodhosts organisation for the current maintained version.
Simple hosts file (/etc/hosts) management in Go (golang).
A Surrealist Parisian Dinner Party chez Madame Rothschild, 1972
- List, add, remove and check hosts file entries from code or the command-line.
- Windows support.
Download the binary and put it in your path.
$ wget -O goodhosts https://github.com/lextoumbourou/goodhosts/releases/download/v2.1.0/goodhosts-linux
$ chmod +x goodhosts
$ export PATH=$(pwd):$PATH
$ goodhosts --helpDownload the binary and do Windowsy stuff with it (doc PR welcome).
$ goodhosts list
127.0.0.1 localhost
10.0.0.5 my-home-server xbmc-server
10.0.0.6 my-desktopAdd --all flag to include comments.
$ goodhosts check 127.0.0.1 facebook.com$ goodhosts add 127.0.0.1 facebook.comOr entries.
$ goodhosts add 127.0.0.1 facebook.com twitter.com gmail.com$ goodhosts rm 127.0.0.1 facebook.comOr entries.
$ goodhosts rm 127.0.0.1 facebook.com twitter.com gmail.com$ goodhosts --help$ go get github.com/lextoumbourou/goodhostspackage main
import (
"fmt"
"github.com/lextoumbourou/goodhosts"
)
func main() {
hosts := goodhosts.NewHosts()
for _, line := range hosts.Lines {
fmt.Println(line.Raw)
}
}package main
import (
"fmt"
"github.com/lextoumbourou/goodhosts"
)
func main() {
hosts := goodhosts.NewHosts()
if hosts.Has("127.0.0.1", "facebook.com") {
fmt.Println("Entry exists!")
return
}
fmt.Println("Entry doesn't exist!")
}package main
import (
"fmt"
"github.com/lextoumbourou/goodhosts"
)
func main() {
hosts := goodhosts.NewHosts()
// Note that nothing will be added to the hosts file until ``hosts.Flush`` is called.
hosts.Add("127.0.0.1", "facebook.com", "twitter.com")
if err := hosts.Flush(); err != nil {
panic(err)
}
}package main
import (
"fmt"
"github.com/lextoumbourou/goodhosts"
)
func main() {
hosts := goodhosts.NewHosts()
// Same deal, yo: call hosts.Flush() to make permanent.
hosts.Remove("127.0.0.1", "facebook.com", "twitter.com")
if err := hosts.Flush(); err != nil {
panic(err)
}
}- Added Windows support.
- Added command-line docs.
- Breaking API change.
- Add support for adding and removing multiple hosts.
- Added
--allflag. - Handle malformed IP addresses.
- Initial release.