Is it possible to get executable file icon by Golang?

Issue

I’m trying to write a "Download Page Website", and I trying to show the file icon to my webpage.

Like Windows system, ".exe" file has icon image inside. Or linux executable file. Can I read it?

I know python can do it with a package name "win32api", is it any package for Golang to achieve this function?

Solution

You can use the linux package in your advantage.

For example, you can use icoextract, which can be installed via apt:

apt install icoextract

And then run it like this:

icoextract /path/to/file.exe /path/to/file.ico

Go make possible to call commands and execute them using the package os/exec. So you can do something like

func ExtractIcon(executablePath string) []byte {
    file, err := ioutil.TempFile("dir", "prefix")
    if err != nil {
        log.Fatal(err)
    }
    defer os.Remove(file.Name())
    cmd := exec.Command("icoextract", executablePath, file.Name())
    if err = cmd.Run(); err != nil {
        log.Fatal(err)
    }

    content, _ := ioutil.ReadFile(file.Name())
    return content
}

Answered By – Jictyvoo

Answer Checked By – Clifford M. (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.