how to ask for administer privileges with Golang

what I want to achieve for my application, is to NOT need to right click and choose “Run as administrator” each time I want to run it, I want Windows to prompt me to gain admin permissions as with other windows applications

consider the following code

package main

import (
    "fmt"
    "io/ioutil"
    "time"
)

func main() {
    err := ioutil.WriteFile("C:/Windows/test.txt", []byte("TESTING!"), 0644)
    if err != nil {
        fmt.Println(err.Error())
        time.Sleep(time.Second * 3)
    }
}

if you compile it and double click on it, it will print:

open: C:Windowstest.txt: Access is denied.

but if you right-click and run as administrator, it will create and write the file

how to make it ask for admin permission just by double clicking on it ?


Source: windows

Leave a Reply