Skip to content

Instantly share code, notes, and snippets.

@mehmetron
Created August 11, 2021 09:20
Show Gist options
  • Save mehmetron/2e21703a9942ff66552ad87772ac26e5 to your computer and use it in GitHub Desktop.
Save mehmetron/2e21703a9942ff66552ad87772ac26e5 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
)
type GoPackage struct {
Path string `json:"Path"`
Version string `json:"Version"`
Indirect bool `json:"Indirect"`
}
type AllPackages struct {
Go string `json:"Go"`
Require []GoPackage `json:"Require"`
}
func GetCmdOutput(cmd []string) []byte {
command := exec.Command(cmd[0], cmd[1:]...)
command.Stderr = os.Stderr
output, err := command.Output()
if err != nil {
fmt.Println(err)
}
return output
}
func main() {
depList := GetCmdOutput([]string{"go", "mod", "edit", "-json"})
fmt.Println("32 ", depList)
fmt.Println("33 ", string(depList))
dynamic := AllPackages{}
json.Unmarshal(depList, &dynamic)
fmt.Println("37 ", dynamic)
fmt.Printf("39 %+v\n", dynamic)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment