SoFunction
Updated on 2025-04-28

Implementation using Go compiled into executable file

Case scenario: Create a two-layer directory, and create a file under this directory, write the "Hello World" character to the file, and read it out.

Objective: (1) Test whether the case can be executed successfully; (2) Compile the code into executable files in two environments: Windows and Linux.

The test code file name is as follows:

package main

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

func main() {
	// Folder name	_dir := "data/test"
	exist, err := pathExists(_dir)
	if err != nil {
		("get dir error![%v]\n", err)
		return
	}

	if exist {
		("has dir![%v]\n", _dir)
	} else {
		("no dir![%v]\n", _dir)
		// Create folder		//err := (_dir, )
		err := (_dir, 0666)
		if err != nil {
			("mkdir failed![%v]\n", err)
		} else {
			("mkdir success!\n")
			fileName := _dir + "/"
			// Create a file			(fileName)
			// Open the file			file, _ := (fileName, os.O_WRONLY|os.O_APPEND, 0666)
			// When the execution is completed, close the file			defer ()
			// Write content into the file			("Hello World!")
			//Read the file			data, _ := (fileName)
			// Print content			(string(data))
		}
	}
}

func pathExists(path string) (bool, error) {
	_, err := (path)
	if err == nil {
		return true, nil
	}
	if (err) {
		return false, nil
	}
	return false, err
}

After debugging, the above code can be executed normally. The test passed.

Compile the executable file process into the Windows environment executable file, open the directory where the file is located, enter cmd in the resource path box, open the cmd command box, and view the current environment variables through "go env". Taking the Windows 10 environment as an example, the default is Windows environment.

// Configure environment variablesSET CGO_ENABLED=1
SET GOOS=windows
SET GOARCH=amd64
// Compile commandsgo build 

After compilation, it is an executable file. It can be directly executed with double-click on the mouse and uploaded to other computers for operation. It can still be executed without relying on third-party packages (unlike Java will rely on JDK).

After execution, in the directory where you are located, generate a data/test/, and open the file to see Hello World.

Compile into a Linux environment executable file. Here, except for the compilation environment parameters, the other steps are similar to the above. The compilation parameters are as follows

//Configuration parametersSET CGO_ENABLED=0 
SET GOOS=linux 
SET GOARCH=amd64 
// Compile commandsgo build 

The compiled and output executable file is named main, uploaded to the system, use "chmod +x main" to add executable permissions, and execute " ./main ". The output result is the same as the above-mentioned Windows results.

Note: The operating systems where the above-mentioned compilation environments are located are Windows 10, that is, the code is developed on Windows 10, and the executable files in both Windows and Linux environments are compiled and output.

This is the end of this article about implementing the method of using Go to compile into executable files. For more related contents of Go compiled into executable files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!