Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b1e22c3

Browse files
updated code
1 parent 4ce9f18 commit b1e22c3

File tree

11 files changed

+353
-0
lines changed

11 files changed

+353
-0
lines changed

‎15-sorting/custom-sorting-struct.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
)
7+
8+
type User struct {
9+
Name string
10+
Age int
11+
}
12+
13+
type UsersByAge []User
14+
15+
func (u UsersByAge) Len() int {
16+
return len(u)
17+
}
18+
func (u UsersByAge) Swap(i, j int) {
19+
u[i], u[j] = u[j], u[i]
20+
}
21+
func (u UsersByAge) Less(i, j int) bool {
22+
return u[i].Age < u[j].Age
23+
}
24+
25+
func main() {
26+
users := []User{
27+
{
28+
Name: "Rajeev",
29+
Age: 28,
30+
},
31+
{
32+
Name: "Monica",
33+
Age: 31,
34+
},
35+
{
36+
Name: "John",
37+
Age: 56,
38+
},
39+
{
40+
Name: "Amanda",
41+
Age: 16,
42+
},
43+
{
44+
Name: "Steven",
45+
Age: 28,
46+
},
47+
}
48+
49+
// Sorting a slice of users by age
50+
sort.Sort(UsersByAge(users))
51+
fmt.Println("Sorted users by age: ", users)
52+
53+
// Sorting a slice of strings in the reverse order of length
54+
sort.Stable(UsersByAge(users))
55+
fmt.Println("[Stable] Sorted users by age: ", users)
56+
57+
}

‎15-sorting/reverse-sorting.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
)
7+
8+
func main() {
9+
// Sorting a slice of Strings
10+
strs := []string{"quick", "brown", "fox", "jumpes"}
11+
sort.Sort(sort.Reverse(sort.StringSlice(strs)))
12+
fmt.Println("Sorted strings in reverse order: ", strs)
13+
14+
// Sorting a slice of Integers
15+
ints := []int{56, 19, 78, 67, 14, 25}
16+
sort.Sort(sort.Reverse(sort.IntSlice(ints)))
17+
fmt.Println("Sorted integers in reverse order: ", ints)
18+
19+
// Sorting a slice of Floats
20+
floats := []float64{176.8, 19.5, 20.8, 57.4}
21+
sort.Sort(sort.Reverse(sort.Float64Slice(floats)))
22+
fmt.Println("Sorted floats in reverse order: ", floats)
23+
}

‎15-sorting/sorting-by-comparator.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
)
7+
8+
func main() {
9+
// Sorting a slice of strings by length
10+
strs := []string{"United States", "India", "France", "United Kingdom", "Spain"}
11+
sort.Slice(strs, func(i, j int) bool {
12+
return len(strs[i]) < len(strs[j])
13+
})
14+
fmt.Println("Sorted strings by length: ", strs)
15+
16+
// Stable sort
17+
sort.SliceStable(strs, func(i, j int) bool {
18+
return len(strs[i]) < len(strs[j])
19+
})
20+
fmt.Println("[Stable] Sorted strings by length: ", strs)
21+
22+
// Sorting a slice of strings in the reverse order of length
23+
sort.SliceStable(strs, func(i, j int) bool {
24+
return len(strs[j]) < len(strs[i])
25+
})
26+
fmt.Println("[Stable] Sorted strings by reverse order of length: ", strs)
27+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
)
7+
8+
type User struct {
9+
Name string
10+
Age int
11+
}
12+
13+
func main() {
14+
// Sorting a slice of structs by a field
15+
users := []User{
16+
{
17+
Name: "Rajeev",
18+
Age: 28,
19+
},
20+
{
21+
Name: "Monica",
22+
Age: 31,
23+
},
24+
{
25+
Name: "John",
26+
Age: 56,
27+
},
28+
{
29+
Name: "Amanda",
30+
Age: 16,
31+
},
32+
{
33+
Name: "Steven",
34+
Age: 28,
35+
},
36+
}
37+
38+
sort.Slice(users, func(i, j int) bool {
39+
return users[i].Age < users[j].Age
40+
})
41+
fmt.Println("Sorted users by age: ", users)
42+
43+
// Stable sort
44+
sort.SliceStable(users, func(i, j int) bool {
45+
return users[i].Age < users[j].Age
46+
})
47+
fmt.Println("Sorted users by age: ", users)
48+
}

‎15-sorting/sorting.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
)
7+
8+
func main() {
9+
// Sorting a slice of Strings
10+
strs := []string{"quick", "brown", "fox", "jumpes"}
11+
sort.Strings(strs)
12+
fmt.Println("Sorted strings: ", strs)
13+
14+
// Sorting a slice of Integers
15+
ints := []int{56, 19, 78, 67, 14, 25}
16+
sort.Ints(ints)
17+
fmt.Println("Sorted integers: ", ints)
18+
19+
// Sorting a slice of Floats
20+
floats := []float64{176.8, 19.5, 20.8, 57.4}
21+
sort.Float64s(floats)
22+
fmt.Println("Sorted floats: ", floats)
23+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"strings"
7+
)
8+
9+
type hobbies []string
10+
11+
func (h *hobbies) String() string {
12+
return fmt.Sprint(*h)
13+
}
14+
15+
func (h *hobbies) Set(value string) error {
16+
for _, hobby := range strings.Split(value, ",") {
17+
*h = append(*h, hobby)
18+
}
19+
return nil
20+
}
21+
22+
func main() {
23+
// Declare a string flag called name with a default value ("Guest") and a help message
24+
name := flag.String("name", "Guest", "specify your name")
25+
26+
// Declare a flag called age with default value of 0 and a help message
27+
age := flag.Int("age", 0, "specify your age")
28+
29+
// Bind the command-line flag with an existing variable
30+
var country string
31+
flag.StringVar(&country, "country", "", "enter your country")
32+
33+
// Defining a custom flag
34+
var hobbiesFlag hobbies
35+
flag.Var(&hobbiesFlag, "hobbies", "comma separated list of hobbies")
36+
37+
// Enable command-line parsing
38+
flag.Parse()
39+
40+
fmt.Printf("Hello %s\n", *name)
41+
fmt.Printf("Your age is %d\n", *age)
42+
fmt.Printf("Your are from %s\n", country)
43+
44+
fmt.Printf("Your hobbies are: ")
45+
for _, hobby := range hobbiesFlag {
46+
fmt.Printf("%s ", hobby)
47+
}
48+
fmt.Println()
49+
50+
// Read all the positional arguments
51+
fmt.Println("Positional Arguments:", flag.Args())
52+
53+
// Read the i'th positional argument
54+
i := 0
55+
fmt.Printf("Positional argument at index %d: %v\n", i, flag.Arg(i))
56+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
s := flag.Int("s", 0, "start line number")
10+
t := flag.Int("t", 0, "end line number")
11+
12+
// Enable command-line parsing
13+
flag.Parse()
14+
15+
fmt.Printf("Search file from line number %d to %d\n", *s, *t)
16+
17+
// Read all the positional arguments
18+
fmt.Println("for keywords:", flag.Args())
19+
20+
// Read the i'th positional argument
21+
i := 0
22+
fmt.Printf("The keyword at index %d: %v\n", i, flag.Arg(i))
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/url"
6+
)
7+
8+
func main() {
9+
// QueryEscape: Escape a string to safely place it inside a URL query string
10+
str := "Gol@ng?&"
11+
encodedStr := url.QueryEscape(str)
12+
13+
fmt.Println(encodedStr)
14+
15+
// PathEscape: Escape a string to safely place it inside a URL path segment
16+
pathVar := "Gol@ng?&"
17+
encodedPathVar := url.PathEscape(pathVar)
18+
fmt.Println(encodedPathVar)
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/url"
6+
)
7+
8+
func main() {
9+
// QueryUnescape: Decode a URL query string
10+
encodedStr := "Gol%40ng%3F%26"
11+
decodedStr, err := url.QueryUnescape(encodedStr)
12+
if err != nil {
13+
fmt.Printf("Error decoding the string %v", err)
14+
}
15+
16+
fmt.Println(decodedStr)
17+
18+
// PathUnescape: Decode a URL path segment
19+
encodedPathVar := "Gol@ng%3F&"
20+
decodedPathVar, err := url.PathUnescape(encodedPathVar)
21+
if err != nil {
22+
fmt.Printf("Error decoding the string %v", err)
23+
}
24+
fmt.Println(decodedPathVar)
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"encoding/base64"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
data := "Gol@ng is Awesome?~"
10+
11+
// Standard Base64 Encoding
12+
encodedData := base64.StdEncoding.EncodeToString([]byte(data))
13+
fmt.Println(encodedData)
14+
15+
// Standard Base64 Encoding without padding
16+
encodedDataWithoutPadding := base64.RawStdEncoding.EncodeToString([]byte(data))
17+
fmt.Println(encodedDataWithoutPadding)
18+
19+
// URL and filename-safe Base64 encoding
20+
urlSafeEncodedData := base64.URLEncoding.EncodeToString([]byte(data))
21+
fmt.Println(urlSafeEncodedData)
22+
23+
// URL and filename-safe Base64 encoding without padding
24+
urlSafeEncodedDataWithoutPadding := base64.RawURLEncoding.EncodeToString([]byte(data))
25+
fmt.Println(urlSafeEncodedDataWithoutPadding)
26+
27+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /