func writeToFile(fileName string, content string) error {
	log.Printf("Writing '%s' into file %s", content, fileName)
	f, err := os.OpenFile(fileName, os.O_WRONLY, os.ModePerm)
	if err != nil {
		return fmt.Errorf("error by opening %s: %v", fileName, err)
	}
	defer f.Close()
	_, err = f.Write([]byte(content))
	if err != nil {
		return fmt.Errorf("error by writing to %s: %v", fileName, err)
	}
	return nil
}
writeToFile writes string into the specified file.
package pci
import (
	"fmt"
	"log"
	"os"
	"strings"
)
const (
	sysBusPCI           = "/sys/bus/pci"
	pciDevVendorFile    = sysBusPCI + "/devices/%s/vendor"
	pciDevIDFile        = sysBusPCI + "/devices/%s/device"
	pciDriverNewDevFile = sysBusPCI + "/drivers/%s/new_id"
	pciDriverBindFile   = sysBusPCI + "/drivers/%s/bind"
	pciDevUnbindFile    = sysBusPCI + "/devices/%s/driver/unbind"
	pciDriverDir        = sysBusPCI + "/drivers/%s"
	pciDriverDevDir     = pciDriverDir + "/%s"
)
const (
	maxReadSize = 1024
)
func DriverBind(pciAddr string, driver string) error {
	if !fileExists(fmt.Sprintf(pciDriverDir, driver)) {
		log.Printf("%s driver is not loaded", driver)
		return fmt.Errorf("%s driver is not loaded", driver)
	}
	if fileExists(fmt.Sprintf(pciDriverDevDir, driver, pciAddr)) {
		log.Printf("%s already bound to the driver %s", pciAddr, driver)
		return nil
	}
	DriverUnbind(pciAddr)  
	log.Printf("Binding %s to driver %s", pciAddr, driver)
	var vendor, devID uint32
	vendorStr, err := readFromFileFile(fmt.Sprintf(pciDevVendorFile, pciAddr))
	if err != nil {
		log.Println(err)
		return err
	}
	fmt.Sscanf(vendorStr, "0x%x", &vendor)
	devIDStr, err := readFromFileFile(fmt.Sprintf(pciDevIDFile, pciAddr))
	if err != nil {
		log.Println(err)
		return err
	}
	fmt.Sscanf(devIDStr, "0x%x", &devID)
	err = writeToFile(fmt.Sprintf(pciDriverNewDevFile, driver),
		fmt.Sprintf("%4x %4x", vendor, devID))
	if err != nil {
		log.Printf("(non-fatal) %s", err)
	}
	err = writeToFile(fmt.Sprintf(pciDriverBindFile, driver), pciAddr)
	if err != nil {
		log.Printf("(non-fatal) %s", err)
	}
	return nil
}
func DriverUnbind(pciAddr string) error {
	log.Printf("Unbinding %s from its current driver", pciAddr)
	err := writeToFile(fmt.Sprintf(pciDevUnbindFile, pciAddr), pciAddr)
	if err != nil {
		log.Println(err)
	}
	return err
}
func fileExists(file string) bool {
	if _, err := os.Stat(file); err == nil {
		return true
	}
	return false
}
func readFromFileFile(fileName string) (string, error) {
	f, err := os.OpenFile(fileName, os.O_RDONLY, os.ModePerm)
	if err != nil {
		return "", fmt.Errorf("error by opening %s: %v", fileName, err)
	}
	defer f.Close()
	buf := make([]byte, maxReadSize)
	_, err = f.Read(buf)
	if err != nil {
		return "", fmt.Errorf("error by reading from %s: %v", fileName, err)
	}
	return strings.TrimSpace(fmt.Sprintf("%s", buf)), nil
}