Created
May 6, 2018 01:35
-
-
Save marcus-crane/cbb98752d060b23f9cc39dbf65d2fe9d to your computer and use it in GitHub Desktop.
Parsing a package.json file into an iterable struct in Go. In this case, it iterates through dependency names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
) | |
type PackageJson struct { | |
Name string `json:"name"` | |
Dependencies Dependency `json:"dependencies"` | |
DevDependencies Dependency `json:"devDependencies"` | |
} | |
type Dependencies struct { | |
Item Dependency | |
} | |
type Dependency map[string]interface{} | |
func main() { | |
var pkgParsed PackageJson | |
pkgJson := []byte(`{"name": "my-cool-package", "dependencies": {"coffee-script": "^4.0.0", "taekwando": "^0.1.0"}, "devDependencies": {"fresh": "^1.2.3"}}`) | |
err := json.Unmarshal(pkgJson, &pkgParsed) | |
if err != nil { | |
panic(err) | |
} | |
for key, _ := range pkgParsed.Dependencies { | |
fmt.Println(key) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment