You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var(sstring ; nint)
fmt.Println("Enter a string and a number : ")
fmt.Scan(&s, &n)
// fmt.Scanln(&s , &n)// fmt.Scanf("%s %d" , s , n)fmr.Printf("You entered : %s and %d\n", s, n)
// scan and scanln are similar the difference is thean scanln end when you enter newline// scan endes when you enter the two variables then space
read from buffer
reader:=bufio.NewReader(os.Stdin)
input,_:=reader.ReadString('\n')
Println("you enter : " , input)
// will read every thing until you enter a new line
take file as a input
file, err:=os.Open("input.txt")
iferr!=nil { // if the file not exist will throw errorfmt.Println(err)
return
}
deferfile.Close()
scanner:=bufio.NewScanner(file)
forscanner.Scan() {
fmt.Println(scanner.Text()) // Print each line
}
iferr:=scanner.Err(); err!=nil {
fmt.Println(err)
}
content, err:=os.ReadFile("input.txt")
iferr!=nil {
fmt.Println(err)
return
}
fmt.Println(string(content)) // Print file content as a string
// Open the file with read/write permissions, create it if it doesn't existfile, err:=os.OpenFile("input.txt", os.O_RDWR|os.O_CREATE, 0666)
iferr!=nil {
fmt.Println("Error opening/creating file:", err)
return
}
deferfile.Close()
fmt.Println("File opened or created successfully")
funcprintMap(cmap[string]string){
forcolor, hex:=rangec{
fmt.Println("Hex code for", color, "is", hex)
}
}
funcmain(){
colors:=map[string]string{
"red": "#ff0000",
"green": "#4bf745",
"white": "#ffffff",
}
printMap(colors)
// Hex code for red is #ff0000// Hex code for green is #4bf745// Hex code for white is #ffffff
}
Map vs struct
Map
Struct
Reference Type
Value Type
all keys must be with same type
all key are just some vars names
all values must be with same type
each value can be with different type
Key are indexed we can iterate throw
key do not support indexing no iteration
do not need to know all the keys at compile
must know all the keys at compile
Interface Folder
why i need Interface
so if i have some thing like that
typeenglishBotstruct{}
typespanishBotstruct{}
func (englishBot) getGreeting() string {
//vert unique logic for generating an english greetingreturn"Hi There!"
}
func (spanishBot) getGreeting() string {
//vert unique logic for generating an spanish greetingreturn"Hola!"
}
/* ERORR HERE 👇*/funcprintGreeting(ebenglishBot){
fmt.Println(eb.getGreeting())
}
funcprintGreeting(sbspanishBot){
fmt.Println(sb.getGreeting())
}
/*Connot declare two functions with same name without unique receiver*/funcmain(){
eb:=englishBot{}
sb:=spanishBot{}
printGreeting(eb)
printGreeting(sb)
}
this is not valid code because you can't declare two functions with the same name of course
if you on java or C# there is something called funcion overloading to do that but not in go
there is solution with receiver and with Interface
solution with interface
typeenglishBotstruct{}
typespanishBotstruct{}
/* new ! 🆕 */typebotinterface {
getGreeting() string
}
/*******/func (englishBot) getGreeting() string {
//vert unique logic for generating an english greetingreturn"Hi There!"
}
func (spanishBot) getGreeting() string//vert unique logic for generating an spanish greetingreturn"Hola!"
}
funcprintGreeting(bbot){
fmt.Println(b.getGreeting())
}
funcmain(){
eb:=englishBot{}
sb:=spanishBot{}
printGreeting(eb)
printGreeting(sb)
}
what i just did is instead of create the same function twice
i make type bot and tell my go programm to notice any type
on my programm and detect if it has on it getGreeting() string
if it has it then automaticly make them also of type bot
that mean i can now use englishBotspanishBot as type bot
and any type has getGreeting() on it also will be of type bot