From b0a83f934ea40a6dd10df824cec19350efc9a41d Mon Sep 17 00:00:00 2001 From: GO Date: Tue, 7 Sep 2021 19:55:43 +0000 Subject: [PATCH] testing --- hello/hello.go | 8 ++++++-- hello/morestrings/reverse.go | 12 ++++++++++++ hello/morestrings/reverse_test.go | 19 +++++++++++++++++++ range/range.go | 31 +++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 hello/morestrings/reverse.go create mode 100644 hello/morestrings/reverse_test.go create mode 100644 range/range.go diff --git a/hello/hello.go b/hello/hello.go index 91cca4f..a56548d 100644 --- a/hello/hello.go +++ b/hello/hello.go @@ -1,7 +1,11 @@ package main -import "fmt" +import ( + "fmt" + "c.infdj.com/GO/courses_golang/hello/morestrings" +) func main() { - fmt.Println("Hello, world.") + // fmt.Println("Hello, world.") + fmt.Println(morestrings.ReverseRunes("!oG ,olleH")) } diff --git a/hello/morestrings/reverse.go b/hello/morestrings/reverse.go new file mode 100644 index 0000000..7f4e24c --- /dev/null +++ b/hello/morestrings/reverse.go @@ -0,0 +1,12 @@ +// Package morestrings implements additional functions to manipulate UTF-8 +// encoded strings, beyond what is provided in the standard "strings" package. +package morestrings + +// ReverseRunes returns its argument string reversed rune-wise left to right. +func ReverseRunes(s string) string { + r := []rune(s) + for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 { + r[i], r[j] = r[j], r[i] + } + return string(r) +} diff --git a/hello/morestrings/reverse_test.go b/hello/morestrings/reverse_test.go new file mode 100644 index 0000000..9a76ef8 --- /dev/null +++ b/hello/morestrings/reverse_test.go @@ -0,0 +1,19 @@ +package morestrings + +import "testing" + +func TestReverseRunes(t *testing.T) { + cases := []struct { + in, want string + }{ + {"Hello, world", "dlrow ,olleH"}, + {"Hello, 世界", "界世 ,olleH"}, + {"", ""}, + } + for _, c := range cases { + got := ReverseRunes(c.in) + if got != c.want { + t.Errorf("ReverseRunes(%q) == %q, want %q", c.in, got, c.want) + } + } +} diff --git a/range/range.go b/range/range.go new file mode 100644 index 0000000..2b06503 --- /dev/null +++ b/range/range.go @@ -0,0 +1,31 @@ +package main + +import "fmt" + +func main() { + nums := []int{2, 3, 4} + sum := 0 + for _, num := range nums { + sum += num + } + fmt.Println("sum:",sum) + + for i,num := range nums { + if num == 3 { + fmt.Println("index:",i) + } + } + + kvs := map[string]string{"a":"apple", "b":"banana"} + for k, v := range kvs { + fmt.Printf("%s -> %s\n", k, v) + } + + for k := range kvs { + fmt.Println("key:", k) + } + + for i,c := range "go" { + fmt.Println(i,c) + } +}