-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
578 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestSortByFunc(t *testing.T) { | ||
|
||
s1 := []string{"xx-1-0", "xx-2-1", "xx-51-0", "xx-51-1", "xx-3-1", "xx-3-0"} | ||
fmt.Println("before sort =", s1) | ||
sortSlice(s1) | ||
fmt.Println("after sort =", s1) | ||
} | ||
|
||
func sortSlice(s1 []string) []string { | ||
sort.Slice(s1, func(i, j int) bool { | ||
split1 := strings.Split(s1[i], "-") | ||
split2 := strings.Split(s1[j], "-") | ||
role1 := split1[len(split1)-1] | ||
role2 := split2[len(split2)-1] | ||
version1 := split1[len(split1)-2] | ||
version2 := split2[len(split2)-2] | ||
if role1 > role2 { | ||
return true | ||
} | ||
if version1 < version2 { | ||
return true | ||
} | ||
return false | ||
}) | ||
return s1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package concurrent | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
// 一般用于超时控制 | ||
func TestTicker(t *testing.T) { | ||
ticker := time.NewTicker(time.Second) | ||
defer ticker.Stop() | ||
done := make(chan bool) | ||
go func() { | ||
time.Sleep(10 * time.Second) | ||
done <- true | ||
}() | ||
for { | ||
select { | ||
case <-done: | ||
fmt.Println("Done!") | ||
return | ||
case t := <-ticker.C: | ||
fmt.Println("Current time: ", t) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package container | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"golang.org/x/sys/unix" | ||
"testing" | ||
) | ||
|
||
// Info reveals information about a particular mounted filesystem. This | ||
// struct is populated from the content in the /proc/<pid>/mountinfo file. | ||
type Info struct { | ||
// ID is a unique identifier of the mount (may be reused after umount). | ||
ID int | ||
|
||
// Parent is the ID of the parent mount (or of self for the root | ||
// of this mount namespace's mount tree). | ||
Parent int | ||
|
||
// Major and Minor are the major and the minor components of the Dev | ||
// field of unix.Stat_t structure returned by unix.*Stat calls for | ||
// files on this filesystem. | ||
Major, Minor int | ||
|
||
// Root is the pathname of the directory in the filesystem which forms | ||
// the root of this mount. | ||
Root string | ||
|
||
// Mountpoint is the pathname of the mount point relative to the | ||
// process's root directory. | ||
Mountpoint string | ||
|
||
// Options is a comma-separated list of mount options. | ||
Options string | ||
|
||
// Optional are zero or more fields of the form "tag[:value]", | ||
// separated by a space. Currently, the possible optional fields are | ||
// "shared", "master", "propagate_from", and "unbindable". For more | ||
// information, see mount_namespaces(7) Linux man page. | ||
Optional string | ||
|
||
// FSType is the filesystem type in the form "type[.subtype]". | ||
FSType string | ||
|
||
// Source is filesystem-specific information, or "none". | ||
Source string | ||
|
||
// VFSOptions is a comma-separated list of superblock options. | ||
VFSOptions string | ||
} | ||
|
||
func (i *Info) String() string { | ||
marshal, _ := json.Marshal(i) | ||
return string(marshal) | ||
} | ||
|
||
func TestGetMountInfo(t *testing.T) { | ||
count, err := unix.Getfsstat(nil, unix.MNT_WAIT) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
entries := make([]unix.Statfs_t, count) | ||
_, err = unix.Getfsstat(entries, unix.MNT_WAIT) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
var out []*Info | ||
for _, entry := range entries { | ||
mountinfo := getMountinfo(&entry) | ||
out = append(out, mountinfo) | ||
} | ||
for _, info := range out { | ||
fmt.Printf("out=%+v\n", info) | ||
} | ||
} | ||
|
||
func getMountinfo(entry *unix.Statfs_t) *Info { | ||
return &Info{ | ||
Mountpoint: unix.ByteSliceToString(entry.Mntonname[:]), | ||
FSType: unix.ByteSliceToString(entry.Fstypename[:]), | ||
Source: unix.ByteSliceToString(entry.Mntfromname[:]), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
### grpcurl 工具 | ||
|
||
参考:https://chai2010.cn/advanced-go-programming-book/ch4-rpc/ch4-08-grpcurl.html | ||
|
||
|
||
1. 安装grpcul工具 | ||
```shell | ||
$ go get github.com/fullstorydev/grpcurl | ||
$ go install github.com/fullstorydev/grpcurl/cmd/grpcurl | ||
``` | ||
2. 查看服务列表 | ||
```shell | ||
$ grpcurl -plaintext localhost:50051 list | ||
HelloService.HelloService | ||
grpc.reflection.v1alpha.ServerReflection | ||
|
||
``` | ||
2. |
Oops, something went wrong.