golang practical programming course:
https://www.youtube.com/watch?v=G3PvTWRIhZA&list=PLQVvvaa0QuDeF3hP0wQoSxpkqgRcgxMqX
#golang #course #youtube
https://www.youtube.com/watch?v=G3PvTWRIhZA&list=PLQVvvaa0QuDeF3hP0wQoSxpkqgRcgxMqX
#golang #course #youtube
YouTube
Introduction - Go Lang Practical Programming Tutorial p.1
Hello and welcome to a Go Language programming tutorial. In this series, we're going to cover setting up and the basics of using Go in a practical way. Go is a programming language where you could easily run through the basics, and then be totally lost when…
os.Rename
in goLang
?Function signature:
func Rename(oldpath, newpath string) error
What
Rename
does?Rename
renames (moves) oldpath to newpath. If newpath already exists and is not a directory, Rename
replaces it. OS-specific restrictions may apply when oldpath and newpath are in different directories. If there is an error, it will be of type *LinkError
.#golang #go #os #rename #os_rename
As you may already know in
In golang you need to use
#python #golang #go #fmt #sprintf #format
Python
you can format your string using format
as below:file_name = "/root/openvpn/{}.ovpn".format(my_file_name)
// Or
file_name = "/root/openvpn/%s.ovpn" % my_file_name
In golang you need to use
Sprintf
method of fmt
package like follow:var fileName = fmt.Sprintf("/root/openvpn/%s.ovpn", myFileName)
#python #golang #go #fmt #sprintf #format
environments in
To set a key/value pair, use
Use
#golang #go #env #environment #environment_variables #range #setenv #getenv #environ
Golang
:To set a key/value pair, use
os.Setenv
. To get a value for a key, use os.Getenv
. This will return an empty string if the key isn’t present in the environment.Use
os.Environ
to list all key/value pairs in the environment. This returns a slice of strings in the form KEY=value
. You can strings.Split
them to get the key and value. Here we print all the keys:package main
import (
"os"
"strings"
"fmt"
)
func main() {
os.Setenv("FOO", "1")
fmt.Println("FOO:", os.Getenv("FOO"))
fmt.Println("BAR:", os.Getenv("BAR"))
fmt.Println()
for _, e := range os.Environ() {
pair := strings.Split(e, "=")
fmt.Println(pair[0])
}
}
#golang #go #env #environment #environment_variables #range #setenv #getenv #environ
If you want to implement "Python Capitalize" method in
There is another method called ToTitle which is used for one character long (but to letters):
#python #golang #go #ToLower #Title #strings #capitalize #ToTitle
Go
:strings.Title(strings.ToLower("MYNAME")) // output: Myname
There is another method called ToTitle which is used for one character long (but to letters):
str := "dz"
fmt.Println(strings.ToTitle(str)) // output: Dz
#python #golang #go #ToLower #Title #strings #capitalize #ToTitle
A very great talk by
- https://www.youtube.com/watch?v=OcjMi9cXItY
#youtube #golang #go #microservice #gomicro #micro
Brian Ketelsen
about microservices using go-micro
:- https://www.youtube.com/watch?v=OcjMi9cXItY
#youtube #golang #go #microservice #gomicro #micro
YouTube
Microservices in Go using Micro - Brian Ketelsen - Codemotion Milan 2017
Go beyond the hype and dig into building microservices with the Go framework Micro (micro.mu). In this talk you’ll learn how to create and deploy microservices using the popular framework Micro, which was extracted out of experience running services at Google…
A very great article about Go styling and how to code readble, maintainable code in Go (it can be used in other languages too):
- https://dave.cheney.net/practical-go/presentations/qcon-china.html#_always_document_public_symbols
#golang #go #maintainable #readble_code
- https://dave.cheney.net/practical-go/presentations/qcon-china.html#_always_document_public_symbols
#golang #go #maintainable #readble_code
dave.cheney.net
Practical Go: Real world advice for writing maintainable Go programs
Let's talk a little bit about
- https://golang.org/pkg/sync/#WaitGroup
A
With an example it will be crystal clear. Let's say we have a script that fetches URLs concurrently and we want to wait using
First we create a
s done getting the URL, defer will be run and
#golang #go #sync #WaitGroup #Done #Add #Wait
WaitGroup
in sync
library. First things first, the documentation is here:- https://golang.org/pkg/sync/#WaitGroup
A
WaitGroup
waits for a collection of goroutines
to finish. The main goroutine
calls Add
to set the number of goroutines
to wait for. Then each of the goroutines
runs and calls Done
when finished. At the same time, Wait
can be used to block until all goroutines
have finished.With an example it will be crystal clear. Let's say we have a script that fetches URLs concurrently and we want to wait using
WaitGroup
until all the fetches are complete:package main
import (
"sync"
)
type httpPkg struct{}
func (httpPkg) Get(url string) {}
var http httpPkg
func main() {
var wg sync.WaitGroup
var urls = []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.somestupidname.com/",
}
for _, url := range urls {
// Increment the WaitGroup counter.
wg.Add(1)
// Launch a goroutine to fetch the URL.
go func(url string) {
// Decrement the counter when the goroutine completes.
defer wg.Done()
// Fetch the URL.
http.Get(url)
}(url)
}
// Wait for all HTTP fetches to complete.
wg.Wait()
}
First we create a
WaitGroup
called wg
and using range
loop through the URLs on each iterration we use Add
to increment WaitGroup
counter/queue and run an anonymous function that gets a URL. When func is done getting the URL, defer will be run and
Done
decrement the counter in WaitGroup
. Finally we use Wait
to actually wait for all http calls to get completed.NOTE:
anonymous function is preceded with go
keyword that runs the function concurrently.#golang #go #sync #WaitGroup #Done #Add #Wait
pkg.go.dev
sync package - sync - Go Packages
Package sync provides basic synchronization primitives such as mutual exclusion locks.
GJSON
is a Go
package that provides a fast and simple way to get values from a json document. It has features such as one line retrieval, dot notation paths, iteration, and parsing json lines.Las mere:
https://github.com/tidwall/gjson
#golang #go #gjson
GitHub
GitHub - tidwall/gjson: Get JSON values quickly - JSON parser for Go
Get JSON values quickly - JSON parser for Go. Contribute to tidwall/gjson development by creating an account on GitHub.