[go] 명령 실행 크로스 플랫폼

Go 언어를 사용하면 손쉽게 크로스 플랫폼 명령을 실행할 수 있습니다. 아래에서는 Go 언어를 사용하여 Windows, macOS 및 Linux에서 명령을 실행하는 방법을 살펴보겠습니다.

Windows에서 명령 실행하기

package main

import (
    "os/exec"
    "fmt"
)

func main() {
    cmd := exec.Command("cmd", "/c", "echo hello")
    output, err := cmd.Output()
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(string(output))
}

macOS 및 Linux에서 명령 실행하기

package main

import (
    "os/exec"
    "fmt"
)

func main() {
    cmd := exec.Command("ls", "-l")
    output, err := cmd.Output()
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(string(output))
}

이제 Go 언어로 각 플랫폼에서 명령을 실행하는 방법을 살펴보았습니다. 이를 통해 효율적으로 크로스 플랫폼 명령 실행을 구현할 수 있습니다.

느낌 폰트 사용: Go 언어 크로스 플랫폼 명령 실행

참고자료