Created
June 10, 2020 15:07
-
-
Save darthsithius/405815ec7fa4278d9c9f54ddcbd25eda to your computer and use it in GitHub Desktop.
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 "fmt" | |
import "os" | |
import "os/exec" | |
import "io" | |
import "strings" | |
func cls(){ | |
cmd := exec.Command("cmd", "/c", "cls") | |
cmd.Stdout = os.Stdout | |
cmd.Run() | |
} | |
type phonebook struct{ | |
name string | |
phone int | |
} | |
func (p phonebook) write_to_file() { | |
f,err := os.OpenFile("phonebook.txt",os.O_APPEND|os.O_CREATE,0666) | |
if err != nil { | |
return | |
} | |
fmt.Fprintf(f,"%s\t%d\n",p.name,p.phone) | |
defer f.Close() | |
} | |
func search_contact(name string) []string{ | |
f,err := os.Open("phonebook.txt") | |
if err != nil { | |
fmt.Println("error") | |
} | |
var x string | |
var y int | |
p_cache := make(map[string]int) | |
for { | |
_,err := fmt.Fscanf(f,"%s\t%d\n",&x,&y) | |
p_cache[x] = y | |
if err == io.EOF { | |
break | |
} | |
} | |
var s []string | |
for n,_ := range(p_cache){ | |
if strings.Contains(n,name){ | |
s=append(s,n) | |
} | |
} | |
return s | |
} | |
func main(){ | |
var x int | |
L: for { | |
fmt.Println("Enter a choice\n1.New Contact\n2.Edit Contact\n3.Search Contact\n4.Delete Contact\n5.Exit") | |
fmt.Scanf("%d\n",&x) | |
switch x { | |
case 1: | |
cls() | |
fmt.Println("Enter name and phone") | |
var n string | |
var ph int | |
fmt.Scanf("%s\n%d\n",&n,&ph) | |
p:=phonebook{n,ph} | |
p.write_to_file() | |
cls() | |
fallthrough | |
case 2: | |
case 3: | |
cls() | |
fmt.Println("Enter name to search") | |
var ( | |
n string | |
slice []string) | |
fmt.Scanf("%s\n",&n) | |
slice = search_contact(n) | |
fmt.Println(slice) | |
fallthrough | |
case 4: | |
case 5: | |
break L | |
default: | |
fmt.Println("Invalid Choice") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
under progress