Skip to content

Instantly share code, notes, and snippets.

@s7anley
Last active February 19, 2016 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s7anley/6f5937524a83cd1000a6 to your computer and use it in GitHub Desktop.
Save s7anley/6f5937524a83cd1000a6 to your computer and use it in GitHub Desktop.
Introduction to Go
package main
import (
"fmt"
)
func main() {
var array [5]float64
array[0] = 98
arrayWithValues := [5]float64{98, 93, 77, 82, 83}
for k, v := range arrayWithValues {
fmt.Println(k, v)
}
age := make(map[string]int)
age["Jany"] = 27
hometown := map[string]string{"Jany": "Prešov"}
for k, v := range age {
fmt.Println(k, v)
}
fmt.Println(hometown)
}
package main
import "fmt"
func countToTen() {
for i := 1; i <= 10; i++ {
fmt.Println(i)
}
}
func countToTwenty() {
for i := 11; i <= 20; i++ {
fmt.Println(i)
}
}
func main() {
go countToTen()
go countToTwenty()
for {
}
}
package main
import (
"fmt"
"time"
)
func pinger(c chan string) {
for i := 0; ; i++ {
c <- "ping"
}
}
func ponger(c chan string) {
for i := 0; ; i++ {
c <- "pong"
}
}
func printer(c chan string) {
for {
msg := <-c
fmt.Println(msg)
time.Sleep(time.Second * 1)
}
}
func main() {
channel := make(chan string)
go pinger(channel)
go ponger(channel)
go printer(channel)
for {
}
}
package main
import (
"fmt"
"os"
)
type errNotExistingClient int
type client struct {
id int
}
func (e errNotExistingClient) Error() string {
return fmt.Sprintf("Cannot obtain client with negative id: %g", int(e))
}
func getClientById(id int) (*client, error) {
if id < 1 {
return nil, errNotExistingClient(id)
}
return &client{id}, nil
}
func main() {
client, err := getClientById(-1)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
fmt.Println(client)
}
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
type request struct {
Name string `json:"name"`
Surname string `json:"surname"`
Age int `json:"age"`
}
func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
var request request
if err := json.Unmarshal(body, &request); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
w.Write([]byte("Hello, " + request.Name + "\n"))
})
log.Fatal(http.ListenAndServe(":8888", nil))
}
package main
import "fmt"
type Speaker interface {
Say(string)
}
type Person struct {
name string
}
func (p *Person) Say(message string) {
fmt.Println(p.name+": ", message)
}
func speak(via Speaker) {
via.Say("abcdefghijklmnopqrstuvwxyz")
}
func main() {
person := &Person{name: "John"}
speak(person)
}
package examples
type client struct {
name *string
}
func (c *client) GetName() string {
return *c.name
}
func NewClient(name string) *client {
return &client{name}
}
package main
import (
"fmt"
)
func divide(x int, y int) (result int, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Integer divide by zero")
result = 0
}
}()
return x / (x - 15), nil
}
func main() {
result, err := divide(15, 30)
if err != nil {
// Error handling
}
fmt.Println(result)
}
package main
import "fmt"
type Client struct {
name *string
}
func NewClient(name string) *Client {
return &Client{name: &name}
}
func main() {
client := NewClient("John Doe")
fmt.Println(client.name)
fmt.Println(*client.name)
}
package main
import (
"fmt"
)
type Person struct {
name string
surname string
}
func (c *Person) GetFullName() string {
return c.name + " " + c.surname
}
func NewPerson(name string, surname string) *Person {
return &Person{name, surname}
}
type Client struct {
*Person
id int
}
func NewClient(name string, surname string, id int) *Client {
return &Client{NewPerson(name, surname), id}
}
func main() {
client := NewClient("John", "Doe", 1)
fmt.Println(client.GetFullName())
}
package examples
import (
"fmt"
"testing"
)
type ClientRepository interface {
save(name string, apiKey string) bool
}
type mockClientRepository struct {
saveFunc func(name string, apiKey string) bool
}
func (mcr *mockClientRepository) save(name string, apiKey string) bool {
return mcr.saveFunc(name, apiKey)
}
type Service struct {
repository ClientRepository
}
func (s *Service) run() error {
if s.repository.save("Rocket Labs", "foobar") {
return nil
}
return fmt.Errorf("Cannot save client")
}
func Test_client_save_should_fail(t *testing.T) {
mockClientRepository := &mockClientRepository{}
mockClientRepository.saveFunc =
func(name string, apiKey string) bool {
return false
}
service := &Service{mockClientRepository}
result := service.run()
if result == nil {
t.Error("Save should fail")
}
}
package main
import "fmt"
func main() {
var hello string = "Hello world!"
var anotherHello = "Hello world agian!"
yetAnotherHello := "Hello world Declaration"
x, y, z := true, 2, "test"
fmt.Println(hello, anotherHello, yetAnotherHello, x, y, z)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment