Created
August 15, 2018 15:37
-
-
Save EchoofthePast/17ac610a1c54b518a4e378462d54b7e1 to your computer and use it in GitHub Desktop.
user input,Readfiles,Writefiles in golang
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 ( | |
"bufio" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"strings" | |
//"log" | |
) | |
type data struct { | |
name string | |
surname string | |
contact string | |
id string | |
count int | |
} | |
var stringsetWM = " <<<<<Welcome>>>>>" | |
var stringsetMM = " <<<<MAIN MENU>>>>" | |
var stringsetBE = " <<<<< >>>>>" | |
var stringsetOM = " <<<< Options >>>>" | |
var stringsetIM = " <<<Enter Input>>>" | |
//I need a user controlled interface for use through out multiple application | |
//||I created a simple and stable user interface | |
//Now I need 3 working inner applications. Control, Read and Write | |
func input(in string) (out string) { | |
fmt.Scanln(&in) | |
out = in | |
return out | |
} | |
func main() { | |
subMain() | |
} | |
func subMain() { | |
var in string | |
//thinking of useing some if and switch code//So far so good on the for switch statement | |
fmt.Println(stringsetBE) | |
fmt.Println(stringsetWM+"\n", stringsetBE+"\n", stringsetIM+"\n", stringsetBE+"\n") | |
//the for loop should start here | |
for { | |
fmt.Println(stringsetOM) | |
sInput := input(in) | |
switch sInput { | |
case "1": | |
option1() | |
//if there is only read a write and build logic then | |
//this option must be something with build logic | |
case "2": | |
//this will be option two. I dont know if i had a choice | |
option2() | |
case "3": | |
// console applications 3 options fmt no http package here | |
option3() | |
//so i can read files and probably ports as well | |
//i can write files and to ports | |
default: | |
//if any other key is inputed this message appears and it waites again | |
fmt.Println("Sorry Command invalid.") | |
} | |
} | |
} | |
//With these the options as part of the base core of the application | |
//the options from then on could be endless | |
func option1() { | |
//this first option is to be part of a control system | |
fmt.Println("This is something special it leans on the power of the command prompt") | |
//lets think! the most important power of the console to talk to ports!! | |
} | |
func option2() { | |
//this second option is to be the read from part | |
fmt.Println("Welcome to option 2. Where you can read a text file") | |
readfile() | |
} | |
func option3() { | |
//This third option is to be the write to part | |
fmt.Println("This option is to write to file as an output some how") | |
writefile() | |
} | |
func readfile() { | |
//readfile was code i made the other day then I pasted it in here | |
//Process : recieve the file name from the user input | |
//Process : read the file that the user has pointed to | |
//Process : read the data and process it in some way | |
var filepath string | |
fmt.Println("Please Enter the Name of the File you are looking for?") | |
fileinput := input(filepath) + ".txt" //works Now | |
file, err := ioutil.ReadFile(fileinput) | |
if err != nil { | |
fmt.Println("Error finding file.", err) | |
} | |
var sFile = string(file) | |
// string converts the target data to string data | |
var theCount int | |
var unSpec int | |
var sKey string | |
var oKey string | |
//first we create scanner as a NewScanner and point it to the sFile | |
scanner := bufio.NewScanner(strings.NewReader(sFile)) | |
//here is where we split the scanner data file to scan word by word | |
scanner.Split(bufio.ScanWords) | |
//declareing oKey data and asking for input from the user | |
fmt.Println("Please Enter the word you are looking for?") | |
oKey = input(sKey) | |
// the for scanner.Scan() will run through each line of the text data and loop | |
//until it reaches the EOF condition as true. | |
for scanner.Scan() { | |
var stringScan = scanner.Text() | |
switch stringScan { | |
case oKey: | |
//Word Counter | |
//Word finder and Counter | |
theCount++ | |
default: | |
unSpec++ | |
} | |
} | |
if theCount < 1 { | |
fmt.Println("Sorry the word you where looking for could not be found") | |
} | |
if theCount > 0 { | |
fmt.Printf("The word %s was found %d time(s).\n", oKey, theCount) | |
} | |
} | |
func writefile() { | |
var name string | |
var in = input(name) | |
file, err := os.Create(in + ".txt") //This code piece should create a file called ... | |
if err != nil { | |
fmt.Println("Failed to create file.", err) | |
} | |
if err == nil { | |
fmt.Println("Created File Successfully.") | |
} | |
defer file.Close() | |
//after creating the file, I need to now write some data to it | |
var path = in + ".txt" | |
// open file using READ & WRITE permission | |
var oFile, error = os.OpenFile(path, os.O_RDWR, 0644) | |
if error != nil { | |
return | |
} | |
defer oFile.Close() | |
var person data | |
person.name = "Name\n" | |
person.surname = "Surname\n" | |
person.id = "ID Number\n" | |
person.contact = "Tel No\n" | |
oFile.WriteString("ATTENTION:\n") | |
oFile.WriteString(person.name) | |
oFile.WriteString(person.surname) | |
oFile.WriteString(string(person.id)) | |
oFile.WriteString(string(person.contact)) | |
// write some text line-by-line to file | |
var hello, _ = oFile.WriteString("Hello Golang Programmer\n") | |
fmt.Println("Hello Here and Hello there", hello) | |
//if err != nil { return } | |
var message = "Example only. Imagine more\n" | |
var msg, _ = oFile.WriteString(message) | |
fmt.Println("Printing..", msg) | |
_, err = oFile.WriteString("This is the end\n") | |
if err != nil { | |
return | |
} | |
// save changes | |
err = file.Sync() | |
if err != nil { | |
return | |
} | |
fmt.Println("==> Done writing to file <==") | |
} | |
//Most likely going to be called writefile() | |
//Then after that there will be |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment