- package main
- import (
- "fmt"
- "log"
- "net"
- "strings"
- )
- import (
- "github.com/lxn/walk"
- . "github.com/lxn/walk/declarative"
- )
- func main() {
- var te *walk.TextEdit
- if _, err := (MainWindow{
- Title: "Test",
- MinSize: Size{300, 200},
- Layout: VBox{},
- Children: []Widget{
- TextEdit{
- AssignTo: &te,
- },
- PushButton{
- Text: "LocalAddr",
- OnClicked: func() {
- text := GetAddr()
- te.SetText(text)
- },
- },
- },
- }).Run(); err != nil {
- log.Fatal(err)
- }
- }
- func GetAddr() string { //Get ip
- conn, err := net.Dial("udp", "baidu.com:80")
- if err != nil {
- fmt.Println(err.Error())
- return "Erorr"
- }
- defer conn.Close()
- return strings.Split(conn.LocalAddr().String(), ":")[0]
- }
- //该片段来自于http://www.codesnippet.cn/detail/140320149029.html
来源: http://www.codesnippet.cn/detail/140320149029.html