Created
October 31, 2017 13:14
-
-
Save nahid/565d74f86debdec8fad244971bf54b87 to your computer and use it in GitHub Desktop.
Go Practice
This file contains 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" | |
func main() { | |
echo := make(chan string, 2) | |
go func() { | |
echo <- "Searching..." | |
echo <- "Find" | |
}() | |
fmt.Println(<- echo) | |
fmt.Println(<- echo) | |
} |
This file contains 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" | |
func client(request chan <- string, msg string) { | |
request <- msg | |
} | |
func server(request <- chan string, response chan <- string) { | |
msg := <- request | |
response <- msg | |
} | |
func main() { | |
request := make(chan string, 1) | |
response := make(chan string, 1) | |
client(request, "Message Passed") | |
server(request, response) | |
fmt.Println(<- response) | |
} |
This file contains 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 "time" | |
func process(done chan bool) { | |
fmt.Println("Starting...") | |
time.Sleep(time.Second + 1) | |
fmt.Println("Processing...") | |
time.Sleep(time.Second) | |
fmt.Println("Done!") | |
done <- true | |
} | |
func main() { | |
done := make(chan bool, 1) | |
go process(done) | |
<- done | |
} |
This file contains 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" | |
func main() { | |
echo := make(chan string) | |
go func() { | |
echo <- "Searching..." | |
}() | |
msg := <- echo | |
fmt.Println(msg) | |
} |
This file contains 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" | |
func show(name string) { | |
for i:=1; i<=10; i++ { | |
fmt.Println(name, ":", i) | |
} | |
} | |
func main() { | |
show("Hello") | |
go show("concurrent") | |
go func(name string) { | |
fmt.Println(name) | |
}("Proccessing...") | |
var input string | |
fmt.Scanln(&input) | |
fmt.Println("Done") | |
} |
This file contains 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" | |
) | |
var mysqlData = make(map[string]string) | |
var redisData = make(map[string]string) | |
type DB interface { | |
conn() string | |
set(key string, val string) bool | |
get(key string) string | |
} | |
type MySQL struct { | |
host string | |
user string | |
pass string | |
port string | |
} | |
type Redis struct { | |
host string | |
port string | |
} | |
func (d MySQL) conn() string { | |
return "MySQL Connected: " + d.user + "@" + d.host + ":" + d.port + " -p " + d.pass | |
} | |
func (d MySQL) set(key string, val string) bool { | |
mysqlData[key] = val | |
return true | |
} | |
func (d MySQL) get(key string) string { | |
return mysqlData[key] | |
} | |
func (d Redis) conn() string { | |
return "Redis Connected: " + d.host + ":" + d.port + " -p " | |
} | |
func (d Redis) set(key string, val string) bool { | |
redisData[key] = val | |
return true | |
} | |
func (d Redis) get(key string) string { | |
return redisData[key] | |
} | |
func Database(db DB) DB { | |
return db | |
} | |
func main() { | |
driver := Redis{} | |
db := Database(driver) | |
db.set("name", "Nahid") | |
db.set("company", "Pathao") | |
fmt.Println(db.get("company")) | |
fmt.Println(mysqlData, redisData) | |
} |
This file contains 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" | |
func main() { | |
chan1 := make(chan string, 1) | |
chan2 := make(chan string, 1) | |
go func() { | |
chan1 <- "Channel 1" | |
}() | |
go func() { | |
chan2 <- "Channel 2" | |
}() | |
for i:=0; i<2; i++ { | |
select { | |
case msg1 := <-chan1: | |
fmt.Println(msg1) | |
case msg2:= <- chan2: | |
fmt.Println(msg2) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment