mirror of
https://github.com/SourceFellows/gobuch.git
synced 2025-09-18 18:14:54 +02:00
initial import
This commit is contained in:
24
best-practices/context-http-server/main.go
Normal file
24
best-practices/context-http-server/main.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func handleCall(res http.ResponseWriter, req *http.Request) {
|
||||
ctx := req.Context()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
res.Write([]byte("end"))
|
||||
fmt.Println("clsoing connection to client")
|
||||
return
|
||||
case <-time.After(20 * time.Second):
|
||||
fmt.Fprintln(res, "Hello World")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", handleCall)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
41
best-practices/context-loop/main.go
Normal file
41
best-practices/context-loop/main.go
Normal file
@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func loop(ctx context.Context) <-chan string {
|
||||
c := make(chan string)
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
close(c)
|
||||
return
|
||||
default:
|
||||
c <- "Hello World!"
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}()
|
||||
return c
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
for s := range loop(ctx) {
|
||||
log.Println(s)
|
||||
}
|
||||
|
||||
log.Println("nächste Variante")
|
||||
|
||||
ctx, cancel = context.WithCancel(context.Background())
|
||||
time.AfterFunc(5*time.Second, cancel)
|
||||
for s := range loop(ctx) {
|
||||
log.Println(s)
|
||||
}
|
||||
|
||||
}
|
28
best-practices/context-value-nono/main.go
Normal file
28
best-practices/context-value-nono/main.go
Normal file
@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type contextKey int
|
||||
|
||||
const usernameKey contextKey = iota
|
||||
|
||||
func businessCall(username string) {
|
||||
//.. was wichtiges
|
||||
fmt.Println(username)
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
vCtx := context.WithValue(ctx, usernameKey, "bob")
|
||||
//.. einiges anderes
|
||||
businessCall(vCtx.Value(usernameKey).(string))
|
||||
|
||||
//Variante2
|
||||
username := "bob"
|
||||
//.. einiges anderes
|
||||
businessCall(username)
|
||||
|
||||
}
|
15
best-practices/context-value/main.go
Normal file
15
best-practices/context-value/main.go
Normal file
@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
|
||||
func main() {
|
||||
var key contextKey = "test"
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, key, "world")
|
||||
fmt.Println(ctx.Value(key))
|
||||
}
|
28
best-practices/context/main.go
Normal file
28
best-practices/context/main.go
Normal file
@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
bg := context.Background()
|
||||
fmt.Println(bg.Deadline()) //0001-01-01 00:00:00 +0000 UTC false
|
||||
fmt.Println(bg.Err()) //<nil>
|
||||
//fmt.Println(<-td.Done()) //blockiert für immer!
|
||||
|
||||
ctx, cancel := context.WithTimeout(bg, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
go func() {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
cancel()
|
||||
}()
|
||||
|
||||
fmt.Println(ctx.Deadline())
|
||||
<-ctx.Done()
|
||||
fmt.Println("Ende")
|
||||
|
||||
}
|
17
best-practices/empty-string-check/main.go
Normal file
17
best-practices/empty-string-check/main.go
Normal file
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
|
||||
s := ""
|
||||
|
||||
if len(s) == 0 {
|
||||
fmt.Println("string ist leer")
|
||||
}
|
||||
|
||||
if s == "" {
|
||||
fmt.Println("string ist leer")
|
||||
}
|
||||
|
||||
}
|
12
best-practices/error-behaviour/external_lib.go
Normal file
12
best-practices/error-behaviour/external_lib.go
Normal file
@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
)
|
||||
|
||||
func callRemoteService() error {
|
||||
|
||||
var err net.UnknownNetworkError = "blub"
|
||||
return err
|
||||
|
||||
}
|
34
best-practices/error-behaviour/main.go
Normal file
34
best-practices/error-behaviour/main.go
Normal file
@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
func isTimeout(err error) bool {
|
||||
|
||||
type timeout interface {
|
||||
Timeout() bool
|
||||
}
|
||||
|
||||
v, ok := err.(timeout)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return v.Timeout()
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := callRemoteService()
|
||||
|
||||
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
|
||||
//nochmal versuchen
|
||||
log.Printf("it's a unknown network error: %v\n", nerr)
|
||||
}
|
||||
|
||||
if err != nil && !isTimeout(err) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
31
best-practices/error-handling-2/main.go
Normal file
31
best-practices/error-handling-2/main.go
Normal file
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var err error
|
||||
var f *os.File
|
||||
|
||||
func write(text string) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = fmt.Fprintln(f, text)
|
||||
}
|
||||
|
||||
func main() {
|
||||
f, err = os.OpenFile("test.txt", os.O_WRONLY, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
write("Hello world 2")
|
||||
write("Hello world 2")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(f)
|
||||
}
|
2
best-practices/error-handling-2/test.txt
Normal file
2
best-practices/error-handling-2/test.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Hello world 2
|
||||
Hello world 2
|
48
best-practices/error-handling-3/main.go
Normal file
48
best-practices/error-handling-3/main.go
Normal file
@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func NewWriter(w io.Writer) *writer {
|
||||
return &writer{w, nil}
|
||||
}
|
||||
|
||||
type writer struct {
|
||||
w io.Writer
|
||||
err error
|
||||
}
|
||||
|
||||
func (w *writer) write(text string) {
|
||||
if w.err != nil {
|
||||
return
|
||||
}
|
||||
_, w.err = fmt.Fprintln(w.w, text)
|
||||
}
|
||||
|
||||
func (w *writer) Err() error {
|
||||
return w.err
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
f, err := os.OpenFile("test.txt", os.O_WRONLY, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
w := NewWriter(f)
|
||||
|
||||
w.write("Hello world 3")
|
||||
w.write("Hello world 3")
|
||||
|
||||
if err = w.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(f)
|
||||
|
||||
}
|
2
best-practices/error-handling-3/test.txt
Normal file
2
best-practices/error-handling-3/test.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Hello world 3
|
||||
Hello world 3
|
24
best-practices/error-handling/main.go
Normal file
24
best-practices/error-handling/main.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
f, err := os.OpenFile("test.txt", os.O_WRONLY, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
_, err = fmt.Fprintln(f, "Hello World")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
_, err = fmt.Fprintln(f, "Hello World")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(f)
|
||||
}
|
4
best-practices/error-handling/test.txt
Normal file
4
best-practices/error-handling/test.txt
Normal file
@ -0,0 +1,4 @@
|
||||
Hello World
|
||||
Hello World
|
||||
Hello World
|
||||
Hello World
|
24
best-practices/file-io-large/main.go
Normal file
24
best-practices/file-io-large/main.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
f, err := os.Open("main.go")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
//scanner.Split(bufio.ScanWords)
|
||||
for scanner.Scan() {
|
||||
fmt.Println(scanner.Text())
|
||||
}
|
||||
|
||||
}
|
24
best-practices/file-io/main.go
Normal file
24
best-practices/file-io/main.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
bites, err := ioutil.ReadFile("test.txt")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Printf("%s\n", bites)
|
||||
fmt.Println(string(bites))
|
||||
|
||||
toWrite := []byte("Hello World!")
|
||||
err = ioutil.WriteFile("test2.txt", toWrite, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
1
best-practices/file-io/test.txt
Normal file
1
best-practices/file-io/test.txt
Normal file
@ -0,0 +1 @@
|
||||
Hello World!
|
18
best-practices/force-interface/main.go
Normal file
18
best-practices/force-interface/main.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
type UserService interface {
|
||||
Save()
|
||||
}
|
||||
|
||||
var _ UserService = &MyUserService{}
|
||||
|
||||
type MyUserService struct {
|
||||
}
|
||||
|
||||
func (us *MyUserService) Save() {
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
33
best-practices/goroutine-pooling/main.go
Normal file
33
best-practices/goroutine-pooling/main.go
Normal file
@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
concurrent = 5
|
||||
pool = make(chan interface{}, concurrent)
|
||||
)
|
||||
|
||||
func work(num int, w *sync.WaitGroup) {
|
||||
pool <- "starting"
|
||||
defer func() {
|
||||
w.Done()
|
||||
<-pool
|
||||
}()
|
||||
fmt.Printf("number %d\n", num)
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 1000; i++ {
|
||||
wg.Add(1)
|
||||
go work(i, &wg)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
}
|
15
best-practices/logging-defer/main.go
Normal file
15
best-practices/logging-defer/main.go
Normal file
@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import "log"
|
||||
|
||||
func importantStuff() {
|
||||
log.Println("rein")
|
||||
defer log.Println("raus")
|
||||
|
||||
log.Println("drin")
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
importantStuff()
|
||||
}
|
14
best-practices/map-contains/main.go
Normal file
14
best-practices/map-contains/main.go
Normal file
@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
|
||||
m := make(map[string]string)
|
||||
m["key1"] = "value1"
|
||||
|
||||
if v, ok := m["key2"]; ok {
|
||||
fmt.Printf("Wert %v enthalten\n", v)
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("myapp")
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("mycli")
|
||||
}
|
BIN
best-practices/project-structure/cmd-sample/myapp
Executable file
BIN
best-practices/project-structure/cmd-sample/myapp
Executable file
Binary file not shown.
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"golang.source-fellows.com/samples/applicationx/http/rest"
|
||||
"golang.source-fellows.com/samples/applicationx/postgres"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
us := &postgres.UserService{}
|
||||
|
||||
http.HandleFunc("/", rest.Handler(us))
|
||||
http.ListenAndServe(":8080", nil)
|
||||
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
module golang.source-fellows.com/samples/applicationx
|
||||
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/golang/mock v1.4.4
|
||||
rsc.io/quote/v3 v3.1.0 // indirect
|
||||
)
|
14
best-practices/project-structure/dependency-sample/go.sum
Normal file
14
best-practices/project-structure/dependency-sample/go.sum
Normal file
@ -0,0 +1,14 @@
|
||||
github.com/golang/mock v1.4.3 h1:GV+pQPG/EUUbkh47niozDcADz6go/dUwhVzdUQHIVRw=
|
||||
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
|
||||
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
|
||||
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/tools v0.0.0-20190425150028-36563e24a262 h1:qsl9y/CJx34tuA7QCPNp86JNJe4spst6Ff8MjvPUdPg=
|
||||
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
|
||||
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
|
@ -0,0 +1,16 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"golang.source-fellows.com/samples/applicationx"
|
||||
)
|
||||
|
||||
func Handler(us applicationx.UserService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
us.CreateUser(&applicationx.User{})
|
||||
fmt.Fprintf(w, "Hello World! %s", time.Now())
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"golang.source-fellows.com/samples/applicationx/mocks"
|
||||
)
|
||||
|
||||
func TestHandler(t *testing.T) {
|
||||
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
userService := mocks.NewMockUserService(ctrl)
|
||||
|
||||
handlerFunc := Handler(userService)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
r, _ := http.NewRequest("GET", "/egal", nil)
|
||||
|
||||
//wird der Service überhaupt aufgerufen?
|
||||
userService.EXPECT().CreateUser(gomock.Any()).MinTimes(1)
|
||||
|
||||
handlerFunc(w, r)
|
||||
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: user.go
|
||||
|
||||
// Package mocks is a generated GoMock package.
|
||||
package mocks
|
||||
|
||||
import (
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
applicationx "golang.source-fellows.com/samples/applicationx"
|
||||
reflect "reflect"
|
||||
)
|
||||
|
||||
// MockUserService is a mock of UserService interface
|
||||
type MockUserService struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockUserServiceMockRecorder
|
||||
}
|
||||
|
||||
// MockUserServiceMockRecorder is the mock recorder for MockUserService
|
||||
type MockUserServiceMockRecorder struct {
|
||||
mock *MockUserService
|
||||
}
|
||||
|
||||
// NewMockUserService creates a new mock instance
|
||||
func NewMockUserService(ctrl *gomock.Controller) *MockUserService {
|
||||
mock := &MockUserService{ctrl: ctrl}
|
||||
mock.recorder = &MockUserServiceMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use
|
||||
func (m *MockUserService) EXPECT() *MockUserServiceMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// CreateUser mocks base method
|
||||
func (m *MockUserService) CreateUser(u *applicationx.User) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "CreateUser", u)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// CreateUser indicates an expected call of CreateUser
|
||||
func (mr *MockUserServiceMockRecorder) CreateUser(u interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateUser", reflect.TypeOf((*MockUserService)(nil).CreateUser), u)
|
||||
}
|
||||
|
||||
// ReadUser mocks base method
|
||||
func (m *MockUserService) ReadUser(id int) (*applicationx.User, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "ReadUser", id)
|
||||
ret0, _ := ret[0].(*applicationx.User)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// ReadUser indicates an expected call of ReadUser
|
||||
func (mr *MockUserServiceMockRecorder) ReadUser(id interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReadUser", reflect.TypeOf((*MockUserService)(nil).ReadUser), id)
|
||||
}
|
||||
|
||||
// DeleteUser mocks base method
|
||||
func (m *MockUserService) DeleteUser(id int) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "DeleteUser", id)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// DeleteUser indicates an expected call of DeleteUser
|
||||
func (mr *MockUserServiceMockRecorder) DeleteUser(id interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteUser", reflect.TypeOf((*MockUserService)(nil).DeleteUser), id)
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.source-fellows.com/samples/applicationx"
|
||||
)
|
||||
|
||||
var _ applicationx.UserService = &UserService{}
|
||||
|
||||
type UserService struct{}
|
||||
|
||||
func (us *UserService) CreateUser(user *applicationx.User) error {
|
||||
fmt.Println("Create User in Postgres Service")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (us *UserService) DeleteUser(id int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (us *UserService) ReadUser(id int) (*applicationx.User, error) {
|
||||
return nil, nil
|
||||
}
|
14
best-practices/project-structure/dependency-sample/user.go
Normal file
14
best-practices/project-structure/dependency-sample/user.go
Normal file
@ -0,0 +1,14 @@
|
||||
//go:generate mockgen -source=user.go -package mocks -destination mocks/user_service.go
|
||||
|
||||
package applicationx
|
||||
|
||||
type User struct {
|
||||
ID int
|
||||
name string
|
||||
}
|
||||
|
||||
type UserService interface {
|
||||
CreateUser(u *User) error
|
||||
ReadUser(id int) (*User, error)
|
||||
DeleteUser(id int) error
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package f
|
||||
|
||||
import "fmt"
|
||||
|
||||
func Access() {
|
||||
fmt.Println("Access it!")
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package c
|
||||
|
||||
import "golang.source-fellows.com/project/isample/a/b/c/internal/d/e/f"
|
||||
|
||||
func Run() {
|
||||
f.Access()
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package g
|
||||
|
||||
import "fmt"
|
||||
|
||||
func Help() {
|
||||
fmt.Println("Help me! cannot access")
|
||||
}
|
3
best-practices/project-structure/internal-sample/go.mod
Normal file
3
best-practices/project-structure/internal-sample/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module golang.source-fellows.com/project/isample
|
||||
|
||||
go 1.14
|
12
best-practices/project-structure/layer-sample/cmd/main.go
Normal file
12
best-practices/project-structure/layer-sample/cmd/main.go
Normal file
@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"layeredsample/models"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c := models.CustomerModel{}
|
||||
fmt.Println(c)
|
||||
}
|
3
best-practices/project-structure/layer-sample/go.mod
Normal file
3
best-practices/project-structure/layer-sample/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module layeredsample
|
||||
|
||||
go 1.14
|
1
best-practices/project-structure/layer-sample/go.sum
Normal file
1
best-practices/project-structure/layer-sample/go.sum
Normal file
@ -0,0 +1 @@
|
||||
golang.source-fellows.com v0.0.0-20200619133407-79aa8981720c h1:c0BKYhs4PULAm3Hb5f+t9AcmPRScdSAkeuajndyOMLQ=
|
@ -0,0 +1,3 @@
|
||||
package handlers
|
||||
|
||||
type CustomerHandler struct{}
|
@ -0,0 +1,3 @@
|
||||
package handlers
|
||||
|
||||
type RegistrationHandler struct{}
|
@ -0,0 +1,11 @@
|
||||
package models
|
||||
|
||||
import "layeredsample/storage"
|
||||
|
||||
type CustomerModel struct {
|
||||
db storage.DB
|
||||
}
|
||||
|
||||
func (c *CustomerModel) Save() {
|
||||
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
package models
|
||||
|
||||
type RegistrationModel struct{}
|
@ -0,0 +1,9 @@
|
||||
package storage
|
||||
|
||||
import "layeredsample/models"
|
||||
|
||||
type DBStorage struct{}
|
||||
|
||||
func (d *DBStorage) Save(c models.CustomerModel) {
|
||||
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
package storage
|
||||
|
||||
type XMLStorage struct{}
|
14
best-practices/project-structure/packagename/cmd/main.go
Normal file
14
best-practices/project-structure/packagename/cmd/main.go
Normal file
@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"golang.source-fellows.com/samples/packagename/util"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
util.ParseValue("Hello World")
|
||||
json.Marshal("Hello World")
|
||||
|
||||
}
|
3
best-practices/project-structure/packagename/go.mod
Normal file
3
best-practices/project-structure/packagename/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module golang.source-fellows.com/samples/packagename
|
||||
|
||||
go 1.14
|
7
best-practices/project-structure/packagename/util/log.go
Normal file
7
best-practices/project-structure/packagename/util/log.go
Normal file
@ -0,0 +1,7 @@
|
||||
package util
|
||||
|
||||
import "fmt"
|
||||
|
||||
func ParseValue(text string) {
|
||||
fmt.Println(text)
|
||||
}
|
28
best-practices/single-method-interface/main.go
Normal file
28
best-practices/single-method-interface/main.go
Normal file
@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Logger interface {
|
||||
Log(message string)
|
||||
}
|
||||
|
||||
type LoggerFunc func(message string)
|
||||
|
||||
func (l LoggerFunc) Log(message string) {
|
||||
l(message)
|
||||
}
|
||||
|
||||
func MyLogFunc(message string) {
|
||||
fmt.Printf("I log %s\n", message)
|
||||
}
|
||||
|
||||
func myMethodTakesTheLog(l Logger) {
|
||||
l.Log("my log message")
|
||||
}
|
||||
|
||||
func main() {
|
||||
logger := LoggerFunc(MyLogFunc)
|
||||
myMethodTakesTheLog(logger)
|
||||
}
|
16
best-practices/struct-to-text/main.go
Normal file
16
best-practices/struct-to-text/main.go
Normal file
@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Tester struct {
|
||||
Name string
|
||||
Alter int
|
||||
Sonstwas string
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
t := &Tester{Name: "Dingo", Alter: 3, Sonstwas: "Wert"}
|
||||
fmt.Printf("%+v\n", t)
|
||||
|
||||
}
|
11
best-practices/synchron-api/main.go
Normal file
11
best-practices/synchron-api/main.go
Normal file
@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
type Reader struct {
|
||||
c chan string
|
||||
}
|
||||
|
||||
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
Reference in New Issue
Block a user