Site icon Composite Thrashing Code

A Go Repo & Some Go Code

Wrapped up another Twitch stream (follow here) and streamed live via YouTube where the video is now (subscribe here, video here). However the more interesting part IMHO was where I broke down a few key parts of the application building features around file writes, reads, JSON marshalling, and some other functionality. I also put together a repo of the code I put together here on Github (code shown and explained below). I dove into this topic in the later part of the stream which I’ve time tagged below. For the full timeline and the rest of the video just watch from the beginning of the video. I do make some progress working on Colligere, but I’ll cover that topic in a subsequent blog entry and Twitch stream.

There were a few notes I took during the session with my collected links and references for things I looked up. Those included the following:

The code for the Github repo, ended in this state. Just a single main.go file, which shows how to use several features of Go and capabilities of the core libraries.

[sourcecode language=”javascript”]
package main

import (
“encoding/json”
“io/ioutil”
“log”
“os”
“os/user”
“strconv”
)

type UserInformation struct {
Name string `json:”name”`
UserId int64 `json:”userid”`
GroupId int64 `json:”groupid”`
HomeDir string `json:”homedir”`
UserName string `json:”username”`
GroupIds []string `json:”groupids”`
GoPath string `json:”gopath”`
EnvVar string `json:”environmentvariable”`
}

func main() {
currentUser, err := user.Current()
check(err)
changeDataForMarshalling(currentUser)
}

func changeDataForMarshalling(currentUser *user.User) {
groupsIds, err := currentUser.GroupIds()
check(err)
userId, err := strconv.ParseInt(currentUser.Uid, 6, 64)
check(err)
groupId, err := strconv.ParseInt(currentUser.Gid, 6, 64)
check(err)

workingUserInformation := &UserInformation{
Name: currentUser.Name,
UserId: userId,
GroupId: groupId,
HomeDir: currentUser.HomeDir,
UserName: currentUser.Username,
GroupIds: groupsIds,
GoPath: os.Getenv(“GOPATH”),
EnvVar: os.Getenv(“NEW_STRING_ENVIRONMENT_VARIABLE”),
}

resultingUserInformation, _ := json.Marshal(workingUserInformation)

filename := “collected_values.json”
if _, err := os.Stat(filename); os.IsNotExist(err) {
writeFileContents(err, filename, string(resultingUserInformation))
} else {
newUserInformation, err := openFileMakeChanges(filename)
writeFileContents(err, filename, newUserInformation)
}
}

func openFileMakeChanges(filename string) (string, error) {
jsonFile, err := os.Open(filename)
check(err)
defer jsonFile.Close()
var changingUserInformation UserInformation
byteValue, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(byteValue, &changingUserInformation)
changingUserInformation.Name = “Adron Hall”
changingUserInformation.GoPath = “/where/is/the/goland”
newUserInformation, _ := json.Marshal(changingUserInformation)
return string(newUserInformation), err
}

func writeFileContents(err error, filename string, text string) {
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0600)
check(err)
defer f.Close()

if _, err = f.WriteString(text); err != nil {
panic(err)
}
}

func check(err error) {
if err != nil {
log.Fatal(err)
}
}
[/sourcecode]

The import section includes several core libraries for the application “encoding/json”, “io/ioutil”, “log”, “os”, “os/user”, and “strconv”. Then I’ve got a structure declared that I use throughout the code with various fields.

Then just for the heck of it I created a changeDataForMarshalling function and passed in userInfo and parsed the string results of gid and uid to int data types. From there the remaining values are passed in then marshalled to JSON and written to a file, depending on if it’s a new file or existing. The writing however I broke out to a function dubbed writeFileContents. Definitely some more refactoring and tweaking to really make it functional and usable, but shows easily what strconv, marshalling, and other features of these core libraries do. This code provides some examples on what functionality Colligere will use to read in schema configuration, edit, and save that schema. More about that in the next Twitch few streams.

47.6835114-122.3986013
Exit mobile version