Last active
October 7, 2020 22:15
-
-
Save stnc/85730dd8fc5c8763c7825f6787a436e1 to your computer and use it in GitHub Desktop.
timer go
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" | |
"io" | |
"io/ioutil" | |
"log" | |
"os" | |
"runtime" | |
"strconv" | |
"time" | |
) | |
// copyFileMacos struct which contains | |
// an array of users | |
type copyFileMacosInıt struct { | |
CopyFileMacos []Data `json:"copyFileMacos"` | |
} | |
// copyFileWindows struct which contains | |
// an array of users | |
type copyFileWindowsInıt struct { | |
CopyFileWindows []Data `json:"copyFileWindows"` | |
} | |
// Users struct which contains | |
// an array of users | |
type copyFileLinuxInıt struct { | |
CopyFileLinux []Data `json:"copyFileLinux"` | |
} | |
// Data struct which contains a name | |
// a type and a list naem soruc | |
type Data struct { | |
Source string `json:"source"` | |
Target string `json:"target"` | |
} | |
//TimeStd strut time list | |
type TimeStd struct { | |
Time int `json:"timeSecond"` | |
} | |
func jSON() { | |
// Open our jsonFile | |
jsonFile, err := os.Open("copy.json") | |
// if we os.Open returns an error then handle it | |
if err != nil { | |
fmt.Println(err) | |
} | |
fmt.Println("Successfully Opened users.json") | |
// defer the closing of our jsonFile so that we can parse it later on | |
defer jsonFile.Close() | |
// read our opened xmlFile as a byte array. | |
byteValue, _ := ioutil.ReadAll(jsonFile) | |
// we initialize our Users array | |
var datasMac copyFileMacosInıt | |
var datasWin copyFileWindowsInıt | |
var datasLinux copyFileLinuxInıt | |
// we unmarshal our byteArray which contains our | |
// jsonFile's content into 'users' which we defined above | |
json.Unmarshal(byteValue, &datasMac) | |
json.Unmarshal(byteValue, &datasWin) | |
json.Unmarshal(byteValue, &datasLinux) | |
var obj TimeStd | |
// unmarshall it | |
err = json.Unmarshal(byteValue, &obj) | |
fmt.Println("Time:" + strconv.Itoa(obj.Time)) | |
// we iterate through every user within our users array and | |
// print out the user Type, their name, and their facebook url | |
// as just an example | |
os := runtime.GOOS | |
switch os { | |
case "darwin": | |
for i := 0; i < len(datasMac.CopyFileMacos); i++ { | |
fmt.Println("----------------------------------------") | |
fmt.Println("Source " + datasMac.CopyFileMacos[i].Source) | |
fmt.Println("Target: " + datasMac.CopyFileMacos[i].Target) | |
copyMethot(datasMac.CopyFileMacos[i].Source, datasMac.CopyFileMacos[i].Target) | |
fmt.Println("----------------------------------------") | |
} | |
case "linux": | |
for i := 0; i < len(datasLinux.CopyFileLinux); i++ { | |
fmt.Println("----------------------------------------") | |
fmt.Println("Source " + datasLinux.CopyFileLinux[i].Source) | |
fmt.Println("Target: " + datasLinux.CopyFileLinux[i].Target) | |
copyMethot(datasLinux.CopyFileLinux[i].Source, datasLinux.CopyFileLinux[i].Target) | |
fmt.Println("----------------------------------------") | |
} | |
case "windows": | |
for i := 0; i < len(datasWin.CopyFileWindows); i++ { | |
fmt.Println("----------------------------------------") | |
fmt.Println("Source " + datasWin.CopyFileWindows[i].Source) | |
fmt.Println("Target: " + datasWin.CopyFileWindows[i].Target) | |
copyMethot(datasWin.CopyFileWindows[i].Source, datasWin.CopyFileWindows[i].Target) | |
fmt.Println("----------------------------------------") | |
} | |
default: | |
fmt.Printf("%s.\n", os) | |
} | |
} | |
//https://tutorialedge.net/golang/parsing-json-with-golang/ | |
func doEvery(d time.Duration, f func(time.Time)) { | |
for x := range time.Tick(d) { | |
f(x) | |
} | |
} | |
func helloworld(t time.Time) { | |
fmt.Printf("%v: Hello, World!\n", t) | |
jSON() | |
} | |
func copyMethot(source string, target string) { | |
// Open original file | |
originalSource, err := os.Open(source) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer originalSource.Close() | |
// Create new file | |
newtTarget, err := os.Create(target) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer newtTarget.Close() | |
//This will copy | |
bytesWritten, err := io.Copy(newtTarget, originalSource) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("Bytes Written: %d\n", bytesWritten) | |
} | |
func main() { | |
doEvery(5*time.Second, helloworld) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
{
}