goodhosts.go
func (h *Hosts) Add(ip string, hosts ...string) error {
	if net.ParseIP(ip) == nil {
		return errors.New(fmt.Sprintf("%q is an invalid IP address.", ip))
	}

	position := h.getIpPosition(ip)
	if position == -1 {
		endLine := NewHostsLine(buildRawLine(ip, hosts))
		// Ip line is not in file, so we just append our new line.
		h.Lines = append(h.Lines, endLine)
	} else {
		// Otherwise, we replace the line in the correct position
		newHosts := h.Lines[position].Hosts
		for _, addHost := range hosts {
			if itemInSlice(addHost, newHosts) {
				continue
			}

			newHosts = append(newHosts, addHost)
		}
		endLine := NewHostsLine(buildRawLine(ip, newHosts))
		h.Lines[position] = endLine
	}

	return nil
}
func (h *Hosts) Add(ip string, hosts ...string) error {
	if net.ParseIP(ip) == nil {
		return errors.New(fmt.Sprintf("%q is an invalid IP address.", ip))
	}

	position := h.getIpPosition(ip)
	if position == -1 {
		endLine := NewHostsLine(buildRawLine(ip, hosts))
		// Ip line is not in file, so we just append our new line.
		h.Lines = append(h.Lines, endLine)
	} else {
		// Otherwise, we replace the line in the correct position
		newHosts := h.Lines[position].Hosts
		for _, addHost := range hosts {
			if itemInSlice(addHost, newHosts) {
				continue
			}

			newHosts = append(newHosts, addHost)
		}
		endLine := NewHostsLine(buildRawLine(ip, newHosts))
		h.Lines[position] = endLine
	}

	return nil
}
func ( h * Hosts ) Add ( ip string , hosts ... string ) error { if net . ParseIP ( ip ) == nil { return errors . New ( fmt . Sprintf ( " " , ip ) ) } position := h . getIpPosition ( ip ) if position == - 1 { endLine := NewHostsLine ( buildRawLine ( ip , hosts ) ) // Ip line is not in file, so we just append our new line. h . Lines = append ( h . Lines , endLine ) } else { // Otherwise, we replace the line in the correct position newHosts := h . Lines [ position ] . Hosts for _ , addHost := range hosts { if itemInSlice ( addHost , newHosts ) { continue } newHosts = append ( newHosts , addHost ) } endLine := NewHostsLine ( buildRawLine ( ip , newHosts ) ) h . Lines [ position ] = endLine } return nil }
// Add an entry to the hosts file.
Add an entry to the hosts file .