SoFunction
Updated on 2025-03-03

How to use Go to detect whether the user is installing chrome locally

Preface

The reason is that I used to open the browser service directly with go, and then yesterday I met a big guy's business. The big guy used python to make a script and packaged it into an exe file, and asked me to help him test it, but an error will be reported on my computer and the execution file cannot be found. However, my computer has Chrome installed, so I asked if it is OK locally, which means that it is a problem with the chrome path in the code. I thought I had encountered the same problem before, so I came up with this article.

This is a tutorial type article. In this tutorial, we will introduce how to use Go to detect if the user system is installedGoogle Chrome (or compatible browsers such as Chromium and Microsoft Edge). This article will describe how to detectMacOS、Window、LinuxHow to install it on the operating system. In addition, if the user does not install, the user will be prompted to downloadChrome

The complete code will be given later.

Why do you need to detect Chrome

Dependency in developmentChromiumWhen the browser performs certain tasks, check whether the application is installedChromeIt's very important. Knowing whether a browser is available helps to provide a seamless experience for users by automatically opening the browser or by instructing users to install it if necessary.

Next let's discuss how to checkChromeand deal with the exact situation.

Step1: Check Chrome with Environment Variables

The fastest way to detect whether a thing is installed is to find environment variables. In some cases, users can specify through environment variablesChromepaths, especially when using a custom browser or configuration.

For example: If you create an environment variableLORCACHROME, you can look up the path first to avoid unnecessary checks.

if path, ok := ("LORCACHROME"); ok {
    if _, err := (path); err == nil {
       return path
    }
}

In the above code,Come and checkLORCACHROMEThe environment changes to give you whether it has been set. If so, use it againVerify that the path is true and valid in the system. If the file exists in the system, it means that the browser is installed in the specified path, we return that path. This judgment provides efficient detection for users who prefer custom configuration paths.

Step2: System-related search paths

Next, you need to expand the search scope beyond the environment variables and search in the default installation pathChromeorChromium. This process varies by operating system because each situation needs to be handled specifically.

MacOS

forMacOSuser,ChromeThe installation is usually located in/Applicationsand possibly installed in/usr/bin, At this time, you need to check these commonly used paths:

paths := []string{
    "/Applications/Google /Contents/MacOS/Google Chrome",
    "/Applications/Google Chrome /Contents/MacOS/Google Chrome Canary",
    "/Applications//Contents/MacOS/Chromium",
    "/Applications/Microsoft /Contents/MacOS/Microsoft Edge",
    "/usr/bin/google-chrome-stable",
    "/usr/bin/google-chrome",
    "/usr/bin/chromium",
    "/usr/bin/chromium-browser",
}

ifChromeInstalling in these default directories returns the first valid path found.

Windows

existWindowsIn the system,ChromeandChromiumUsually installed inProgram FilesorLocalAppDataIn the directory. This code checks the 64-bit and 32-bit program directories as well as Edge:

paths := []string{
    ("LocalAppData") + "/Google/Chrome/Application/",
    ("ProgramFiles") + "/Google/Chrome/Application/",
    ("ProgramFiles(x86)") + "/Google/Chrome/Application/",
    ("LocalAppData") + "/Chromium/Application/",
    ("ProgramFiles") + "/Chromium/Application/",
    ("ProgramFiles(x86)") + "/Chromium/Application/",
    ("ProgramFiles(x86)") + "/Microsoft/Edge/Application/",
    ("ProgramFiles") + "/Microsoft/Edge/Application/",
}

Use here()GetProgramFilesandLocalAppDataetc. The values ​​of environment variables are important system-specific paths.

Linux

existLinuxIt may be installed through package management or SnapChrome or Chromium. These paths are the standard paths for many distributions:

paths = []string{
    "/usr/bin/google-chrome-stable",
    "/usr/bin/google-chrome",
    "/usr/bin/chromium",
    "/usr/bin/chromium-browser",
    "/snap/bin/chromium",
}

Here, check/usr/binand/snap/binbinaries in, they are common locations for Linux applications.

Step3: Check if Chrome exists

Now there is a list of potential paths for each operating system, conveniently check them by looping throughChromeDoes it exist:

for _, path := range paths {
    if _, err := (path); (err) {
       continue
    }
    return path
}

In the above code, each list path is traversed usingTo verify that the file exists. If so, return the path immediately. If not, then proceed to check the next path. If none of them contain valid installations, an empty string will be returned

Step4: Handle Chrome not installed

What to do if Chrome is not installed on the user system? We can handle this by prompting the user to download and install Chrome. We define a function to open the Chrome page in the user's default browser:

func PromptDownload() {
    // Open download page
    url := "/chrome/"
    switch  {
    case "linux":
       ("xdg-open", url).Run()
    case "darwin":
       ("open", url).Run()
    case "windows":
       r := ("&", "^&")
       ("cmd", "/c", "start", (url)).Run()
    }
}

This function constructs the URL of the Chrome download page, and then uses the system commands of each operating system to open the link:

  • MacOS:openThe command opens url in the default browser.
  • Windows: cmdCommand usagestartOpen url.
  • Linux:xdg-openThe command is used to open the URL by default.

By placing these codes, you can seamlessly redirect to the page where the user installs Chrome.

Complete code

package main

import (
    "os"
    "os/exec"
    "runtime"
    "strings"
)

func LocateChrome() string {
    if path, ok := ("LORCACHROME"); ok {
       if _, err := (path); err == nil {
          return path
       }
    }

    var paths []string
    switch  {
    case "darwin":
       paths = []string{
          "/Applications/Google /Contents/MacOS/Google Chrome",
          "/Applications/Google Chrome /Contents/MacOS/Google Chrome Canary",
          "/Applications//Contents/MacOS/Chromium",
          "/Applications/Microsoft /Contents/MacOS/Microsoft Edge",
          "/usr/bin/google-chrome-stable",
          "/usr/bin/google-chrome",
          "/usr/bin/chromium",
          "/usr/bin/chromium-browser",
       }
    case "windows":
       paths = []string{
          ("LocalAppData") + "/Google/Chrome/Application/",
          ("ProgramFiles") + "/Google/Chrome/Application/",
          ("ProgramFiles(x86)") + "/Google/Chrome/Application/",
          ("LocalAppData") + "/Chromium/Application/",
          ("ProgramFiles") + "/Chromium/Application/",
          ("ProgramFiles(x86)") + "/Chromium/Application/",
          ("ProgramFiles(x86)") + "/Microsoft/Edge/Application/",
          ("ProgramFiles") + "/Microsoft/Edge/Application/",
       }
    default:
       paths = []string{
          "/usr/bin/google-chrome-stable",
          "/usr/bin/google-chrome",
          "/usr/bin/chromium",
          "/usr/bin/chromium-browser",
          "/snap/bin/chromium",
       }
    }

    for _, path := range paths {
       if _, err := (path); (err) {
          continue
       }
       return path
    }
    return ""
}

func PromptDownload() {
    // Open download page
    url := "/chrome/"
    switch  {
    case "linux":
       ("xdg-open", url).Run()
    case "darwin":
       ("open", url).Run()
    case "windows":
       r := ("&", "^&")
       ("cmd", "/c", "start", (url)).Run()
    }
}

Summarize

This tutorial describes the steps involved in using Go to detect Chrome installations across multiple operating systems. We first checked the environment variables, then searched for the system-related paths installed by commonly used browsers, and finally handled the situation where Chrome is not installed by prompting the user to download.

This is the article about how to use Go to detect whether users are installing Chrome locally. For more related content on Go to detect whether chrome is installed locally, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future.