【Golang】制作一个信息查询GUI
介绍
- golang函数采集windows主机配置返回string结果,vue js调用并将放回的值赋值到DOM上展示
- 使用wails框架
示例开发一个查询windows主机信息的小程序
- 后端代码
func (a *App) GetHostname() string {
hostname, _ := os.Hostname()
return fmt.Sprintf("主机名 %s\n", hostname)
}
func (a *App) GetIP() string {
interfaces, err := net.Interfaces()
if err != nil {
return err.Error()
}
for _, iface := range interfaces {
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
continue // 忽略未激活的或环回接口
}
addrs, err := iface.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // 不是IPv4地址
}
//return
return fmt.Sprintf("IPV4地址 %s\n ", ip.String())
}
}
return fmt.Sprintf("未发现ipv4地址")
}
// 获取C盘
func (a *App) GetDisk() string {
var freeBytesAvailable int64
h := windows.MustLoadDLL("kernel32.dll")
c := h.MustFindProc("GetDiskFreeSpaceExW")
_, _, err := c.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr("C:\\"))),
uintptr(unsafe.Pointer(&freeBytesAvailable)),
0, // 我们不关心总字节数,所以传递一个null指针
0, // 我们也不关心总可用字节数,所以也传递一个null指针
)
if err != nil && err.Error() != "The operation completed successfully." {
return err.Error()
}
return fmt.Sprintf("C盘可用空间 %.2f GB\n", float64(freeBytesAvailable)/(1024*1024*1024))
}
- front.vue
<script setup>
import {reactive} from 'vue'
import {GetHostname, GetIP, GetDisk} from '../../wailsjs/go/main/App'
const data = reactive({
name: "",
resultText: "点击下方按钮获取主机名 👇",
resultText2: "点击下方按钮获取IP 👇",
resultText3: "点击下方按钮获取C盘剩余空间 👇",
})
function getHostname() {
GetHostname().then(result => {
data.resultText = result
})
}
function getIP() {
GetIP().then(result => {
data.resultText2 = result
})
}
function getDisk() {
GetDisk().then(result => {
data.resultText3 = result
})
}
</script>
<template>
<main style="overflow: hidden;">
<!-- 主机名-->
<div id="result" class="result">{{ data.resultText }}</div>
<div id="input" class="input-box">
<!-- <input id="name" v-model="data.name" autocomplete="off" class="input" type="text"/>-->
<button class="btn" @click="getHostname">主机名</button>
</div>
<!-- ip-->
<div id="result" class="result">{{ data.resultText2 }}</div>
<div id="input" class="input-box">
<button class="btn" @click="getIP">IP地址</button>
</div>
<!--disk-->
<div id="result" class="result">{{ data.resultText3 }}</div>
<div id="input" class="input-box">
<button class="btn" @click="getDisk">C盘可用空间大小</button>
</div>
</main>
</template>
<style scoped>
.result {
height: 20px;
line-height: 20px;
margin: 1.5rem auto;
}
.input-box .btn {
width: 120px;
height: 30px;
line-height: 30px;
border-radius: 3px;
border: none;
margin: 0 0 0 20px;
padding: 0 8px;
cursor: pointer;
}
.input-box .btn:hover {
background-image: linear-gradient(to top, #cfd9df 0%, #e2ebf0 100%);
color: #333333;
}
.input-box .input {
border: none;
border-radius: 3px;
outline: none;
height: 30px;
line-height: 30px;
padding: 0 10px;
background-color: rgba(240, 240, 240, 1);
-webkit-font-smoothing: antialiased;
}
.input-box .input:hover {
border: none;
background-color: rgba(255, 255, 255, 1);
}
.input-box .input:focus {
border: none;
background-color: rgba(255, 255, 255, 1);
}
</style>
转载请注明-MrZ-个人博客
THE END
1
二维码
海报
【Golang】制作一个信息查询GUI
介绍
golang函数采集windows主机配置返回string结果,vue js调用并将放回的值赋值到DOM上展示
使用wails框架
示例开发一个查询windows主机信息的小程序
后端代码
func (a *App) GetHostname() string {
h……

共有 0 条评论