如何确保计算机已连接到互联网并启动执行?

huangapple go评论73阅读模式
英文:

How to make sure the PC is having internet and then launch exec?

问题

我有一个正在运行的应用程序,当Windows 8.1启动时,它会自动启动。但是经常出现电脑稍后连接到网络,结果Google Chrome显示加载失败的页面。

package main
import "os"
import "os/exec"
import "runtime"
import "encoding/json"

type Configuration struct {
  main []string
  name []string
  window []string
}

func main() {
  myos := runtime.GOOS;
  myarch := runtime.GOARCH;
  var chrome = "";
  var cmdopen *exec.Cmd;

  if myos == "windows" {
    if myarch == "386" {
      chrome = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe";
    } else {      
      chrome = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe";
    }          

    // 读取配置
    file, _ := os.Open("C:/Program Files (x86)/abc/package.json");
    decoder := json.NewDecoder(file);
    configuration := Configuration{};
    err := decoder.Decode(&configuration);
    if err != nil {
      println("error: ", err);
    }

    println(configuration.main);

    // BUG!!!!!!!!!!!!!!!!!!! 但是请确保本地网络或互联网可用,不要像白痴一样直接执行Chrome,显示一个无效的页面
    cmdopen = exec.Command(chrome, "--app=http://icanhazip.com");
    err1 := cmdopen.Start();
    if err1 != nil {
      println("Failed: ", err1);
    } 

  } else {
    println("Incompatible");
  } 
}

以上是要翻译的内容。

英文:

I have this application running, when system boots Windows 8.1 then it launch. But often the PC get into the network later as a result Google Chrome shows a failed page.

package main
import "os"
import "os/exec"
import "runtime"
import "encoding/json"

type Configuration struct {
  main []string
  name []string
  window []string
}

func main() {
  myos := runtime.GOOS;
  myarch := runtime.GOARCH;
  var chrome = "";
  var cmdopen *exec.Cmd;

  if myos == "windows" {
    if myarch == "386" {
      chrome = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe";
    } else {      
      chrome = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe";
    }          

    // Read config
    file, _ := os.Open("C:/Program Files (x86)/abc/package.json");
    decoder := json.NewDecoder(file);
    configuration := Configuration{};
    err := decoder.Decode(&configuration);
    if err != nil {
      println("error: ", err);
    }

    println(configuration.main);

    // BUG!!!!!!!!!!!!!!!!!!! But make sure local network or internet is available do not just execute the chrome like idiot, which is showing dead page
    cmdopen = exec.Command(chrome, "--app=http://icanhazip.com");
    err1 := cmdopen.Start();
    if err1 != nil {
      println("Failed: ", err1);
    } 

  } else {
    println("Incompatible");
  } 
}

答案1

得分: 1

你可以使用http.Get()函数来执行此操作。

func hazInternet() bool {
    res, err := http.Get("http://www.google.com/robots.txt")
    if err != nil {
        log.Println(err)
        return false
    }
    res.Body.Close()
    return true
}

Go PlayGround

英文:

you could do an http.Get()

func hazInternet() bool {
	res, err := http.Get("http://www.google.com/robots.txt")
	if err != nil {
		log.Println(err)
		return false
	}
	res.Body.Close()
	return true
}

Go PlayGround

huangapple
  • 本文由 发表于 2015年5月11日 05:11:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/30156703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定