Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • schmolli/examples-go
1 result
Show changes
Commits on Source (5)
Showing with 178 additions and 18 deletions
main
*.pb.go
.DS_Store
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
}
]
}
\ No newline at end of file
go 1.20
use ./src/gocat
use ./src/gofabric
use ./src/gojoke
use ./src/gomd5
use ./src/gopalindrome
use ./src/gopraha
use ./src/goproxy
use ./src/gouni
use ./src/gourl
use ./src/hello-grpc/client
use ./src/hello-grpc/server
use ./src/hello-kafka
use ./src/gowww/client
use ./src/gowww/server
module github.com/turngeek/examples-go/src/gocat
go 1.16
go 1.20
module github.com/turngeek/examples-go/src/gofabric
go 1.20
module github.com/turngeek/examples-go/src/gojoke
go 1.16
go 1.20
......@@ -8,7 +8,7 @@ import (
)
const (
url = "http://api.icndb.com/jokes/random"
url = "https://api.chucknorris.io/jokes/random"
)
func main() {
......@@ -23,5 +23,5 @@ func main() {
if err != nil {
log.Fatalf("Bad response\n:#%v", err)
}
log.Printf("Joke of the Day:\n%s", jokeResponse.Value.Joke)
log.Printf("Joke of the Day:\n%s", jokeResponse.Value)
}
package main
type JokeResponse struct {
Type string `json:"type"`
Value Value `json:"value"`
}
type Value struct {
Id int `json:"id"`
Joke string `json:"joke"`
Id string `json:"id"`
Value string `json:"value"`
Categories []string `json:"categories"`
}
module github.com/turngeek/examples-go/src/gopalindrome
go 1.16
go 1.20
package main
import (
"testing"
)
func TestIsPalindrome(t *testing.T) {
tests := []struct {
str string
expected bool
}{
{
"Reit nie, ein Tier",
true,
},
{
"Kein Palidrom",
false,
},
{
"Abba",
true,
},
}
for _, tt := range tests {
t.Run(tt.str, func(t *testing.T) {
isPalindrome := isPalindrome(tt.str)
if isPalindrome != tt.expected {
t.Errorf(`IsPalindrome("%s") returns %v but must be %v`, tt.str, isPalindrome, tt.expected)
}
})
}
}
module github.com/turngeek/examples-go/src/gopraha
go 1.16
go 1.20
require github.com/segmentio/kafka-go v0.4.16 // indirect
module github.com/turngeek/examples-go/src/goproxy
go 1.16
go 1.20
require github.com/gorilla/mux v1.8.0 // indirect
module github.com/turngeek/examples-go/src/gouni
go 1.16
go 1.20
module github.com/turngeek/examples-go/src/gourl
go 1.16
go 1.20
package main
import (
"bytes"
"encoding/json"
"log"
"net/http"
)
const (
url = "http://localhost:8080/hellowww"
)
type helloWWWRequest struct {
Name string `json:"name"`
}
type helloWWWResponse struct {
Message string `json:"message"`
}
func main() {
log.Printf("Client is sending a request to %v\n", url)
request := helloWWWRequest{Name: "Marcus"}
data, err := json.Marshal(&request)
if err != nil {
log.Fatalf("Error while marshalling request: %v", err)
}
resp, err := http.Post(url, "application/json", bytes.NewBuffer(data))
if err != nil {
log.Fatalf("Error while sending request %s: %v", url, err)
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var response helloWWWResponse
err = decoder.Decode(&response)
if err != nil {
log.Fatalf("Error reading from %s\n%#v", url, err)
}
log.Printf("Message: %s", response.Message)
}
module github.com/turngeek/examples-go/src/gowww/client
go 1.20
module github.com/turngeek/examples-go/src/gowww/server
go 1.20
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
type helloWWWRequest struct {
Name string `json:"name"`
}
type helloWWWResponse struct {
Message string `json:"message"`
}
func main() {
port := 8080
r := mux.NewRouter()
r.HandleFunc("/hellowww", helloWWWHandler).Methods("GET")
r.HandleFunc("/hellowww", helloYouHandler).Methods("POST")
log.Printf("Starting server on port %v\n", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), r))
}
func helloWWWHandler(w http.ResponseWriter, r *http.Request) {
response := helloWWWResponse{Message: "Hello WWW"}
encoder := json.NewEncoder(w)
encoder.Encode(&response)
}
func helloYouHandler(w http.ResponseWriter, r *http.Request) {
var request helloWWWRequest
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&request)
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
response := helloWWWResponse{Message: "Hello " + request.Name}
encoder := json.NewEncoder(w)
encoder.Encode(&response)
}
module github.com/turngeek/examples-go/src/hello-grpc/client
go 1.16
go 1.20
require (
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
......
module github.com/turngeek/examples-go/src/hello-grpc/server
go 1.16
go 1.20
require (
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
......