proceed to learn go!

This commit is contained in:
a.pivkin
2025-06-30 06:58:29 +03:00
parent 498d83835a
commit 6f809ab005
24 changed files with 567 additions and 15 deletions
+3
View File
@@ -0,0 +1,3 @@
module mytypes
go 1.24.4
+35
View File
@@ -0,0 +1,35 @@
package mytypes
import "strings"
type MyInt int
type MyString string
type MyBuilder struct {
Contents strings.Builder
}
func (i MyInt) Twice() MyInt {
return i * 2
}
func (i MyString) Len() int {
return len(i)
}
func (s MyBuilder) Hello() string {
return "Hello, Gophers!"
}
func (s MyBuilder) Uppers(in string) string {
return strings.ToUpper(in)
}
func Double(input *int) int{
*input *= 2
return *input
}
func (input *MyInt) Double() {
*input *= 2
}
+107
View File
@@ -0,0 +1,107 @@
package mytypes_test
import (
"mytypes"
"testing"
"strings"
)
func TestTwice(t *testing.T) {
t.Parallel()
input := mytypes.MyInt(9)
want := mytypes.MyInt(18)
got := input.Twice()
if want != got {
t.Errorf("twice %d: want %d, got %d", input, want,
got)
}
}
func TestMyStringLen(t *testing.T) {
t.Parallel()
input := mytypes.MyString("lol")
want := 3
got := input.Len()
if want != got {
t.Errorf("%q: want %d, got %d", input, want, got)
}
}
func TestStringsBuilder(t *testing.T) {
t.Parallel()
var sb strings.Builder
sb.WriteString("Hello, ")
sb.WriteString("Gophers!")
want := "Hello, Gophers!"
got := sb.String()
if want != got {
t.Errorf("want %q, got %q", want, got)
}
wantLen := 15
gotLen := sb.Len()
if wantLen != gotLen {
t.Errorf("%q: want len %d, got %d", sb.String(),
wantLen, gotLen)
}
}
// func TestMyBuilderLen(t *testing.T) {
// t.Parallel()
// var mb mytypes.MyBuilder
// want := 15
// got := mb.Len()
// if want != got {
// t.Errorf("want %d, got %d", want, got)
// }
// }
func TestMyBuilderHello(t *testing.T) {
t.Parallel()
var mb mytypes.MyBuilder
want := "Hello, Gophers!"
got := mb.Hello()
if want != got {
t.Errorf("want %q, got %q", want, got)
}
}
func TestMyBuilder(t *testing.T) {
t.Parallel()
var mb mytypes.MyBuilder
mb.Contents.WriteString("Hello, ")
mb.Contents.WriteString("Gophers!")
want := "Hello, Gophers!"
got := mb.Contents.String()
if want != got {
t.Errorf("want %q, got %q", want, got)
}
wantLen := 15
gotLen := mb.Contents.Len()
if wantLen != gotLen {
t.Errorf("%q: want len %d, got %d",
mb.Contents.String(), wantLen, gotLen)
}
}
func TestUpperCase(t *testing.T) {
t.Parallel()
var mb mytypes.MyBuilder
input := "hello"
want := "HELLO"
got := mb.Uppers(input)
if got != want {
t.Errorf("%q: want %s, got %s",input,want,got)
}
}
func TestDouble(t *testing.T) {
t.Parallel()
x := mytypes.MyInt(12)
want := mytypes.MyInt(24)
p := &x
p.Double()
if want != x {
t.Errorf("want %d, got %d",want,x)
}
}