Skip to content

Commit

Permalink
doc: update doc for RandNumberOfLength and GetExeOrDllVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
duke-git committed Oct 18, 2024
1 parent ed93aae commit 0a2cc9c
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 24 deletions.
11 changes: 6 additions & 5 deletions docs/api/packages/fileutil.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ import (
- [ReadFile](#ReadFile)
- [ChunkRead](#ChunkRead)
- [ParallelChunkRead](#ParallelChunkRead)
- [GetVersion](#Version)
- [GetExeOrDllVersion](#GetExeOrDllVersion)

<div STYLE="page-break-after: always;"></div>

## 文档
Expand Down Expand Up @@ -1077,17 +1078,17 @@ func main() {
// 2
}
```
### <span id="Version">GetExeDllVersion</span>
### <span id="GetExeOrDllVersion">GetExeOrDllVersion</span>

<p>返回exe,dll文件版本号(仅Window平台).</p>

<b>函数签名:</b>

```go
func GetExeDllVersion(filePath string) (string, error)
func GetExeOrDllVersion(filePath string) (string, error)
```

<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/s_Tl7lZoAaY)</span></b>
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>

```go
package main
Expand All @@ -1098,7 +1099,7 @@ import (
)

func main() {
v, err := fileutil.GetExeDllVersion(`C:\Program Files\Tencent\WeChat\WeChat.exe`)
v, err := fileutil.GetExeOrDllVersion(`C:\Program Files\Tencent\WeChat\WeChat.exe`)
if err != nil {
panic(err)
}
Expand Down
12 changes: 6 additions & 6 deletions docs/api/packages/random.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
- [RandStringSlice](#RandStringSlice)
- [RandBool](#RandBool)
- [RandBoolSlice](#RandBoolSlice)
- [RandNumLen](#RandNumLen)
- [RandNumberOfLength](#RandNumberOfLength)


<div STYLE="page-break-after: always;"></div>
Expand Down Expand Up @@ -524,17 +524,17 @@ func main() {
fmt.Println(result) // [true false] (random)
}
```
### <span id="RandNumLen">RandNumLen</span>
### <span id="RandNumberOfLength">RandNumberOfLength</span>

<p>生成指定长度的随机数</p>

<b>函数签名:</b>

```go
func RandNumLen(len int) int
func RandNumberOfLength(len int) int
```

<b>实例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/o-VSjPjnILI)</span></b>
<b>实例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>

```go
package main
Expand All @@ -545,7 +545,7 @@ import (
)

func main() {
i := random.RandNumLen(2)
fmt.Println(i)
i := random.RandNumberOfLength(2)
fmt.Println(i) // 21 (random number of length 2)
}
```
34 changes: 34 additions & 0 deletions docs/en/api/packages/fileutil.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
- [ReadFile](#ReadFile)
- [ChunkRead](#ChunkRead)
- [ParallelChunkRead](#ParallelChunkRead)
- [GetExeOrDllVersion](#GetExeOrDllVersion)

<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -1074,4 +1075,37 @@ func main() {
// Jim,21,male
// 2
}
```

### <span id="GetExeOrDllVersion">GetExeOrDllVersion</span>

<p>Get the version of exe or dll file on windows.</p>

<b>Signature:</b>

```go
func GetExeOrDllVersion(filePath string) (string, error)
```

<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>

```go
package main

import (
"fmt"
"github.com/duke-git/lancet/v2/fileutil"
)

func main() {
v, err := fileutil.GetExeOrDllVersion(`C:\Program Files\Tencent\WeChat\WeChat.exe`)
if err != nil {
panic(err)
}

fmt.Println(v)

// Output:
// 3.9.10.19
}
```
27 changes: 27 additions & 0 deletions docs/en/api/packages/random.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
- [RandStringSlice](#RandStringSlice)
- [RandBool](#RandBool)
- [RandBoolSlice](#RandBoolSlice)
- [RandNumberOfLength](#RandNumberOfLength)

<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -523,4 +524,30 @@ func main() {
result := random.RandBoolSlice(2)
fmt.Println(result) // [true false] (random)
}
```

### <span id="RandNumberOfLength">RandNumberOfLength</span>

<p>Generates a random int number of specified length.</p>

<b>Signature:</b>

```go
func RandNumberOfLength(len int) int
```

<b>Signature:<span style="float:right;display:inline-block;">[Run](todo)</span></b>

```go
package main

import (
"fmt"
"github.com/duke-git/lancet/v2/random"
)

func main() {
i := random.RandNumberOfLength(2)
fmt.Println(i) // 21 (random number of length 2)
}
```
6 changes: 4 additions & 2 deletions fileutil/file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ type tagVS_FIXEDFILEINFO struct {
FileDateLS uint32
}

// GetExeDllVersion 获取exe或dll文件的版本信息
func GetExeDllVersion(filePath string) (string, error) {
// GetExeOrDllVersion get the version of exe or dll file on windows.
// Play: todo
func GetExeOrDllVersion(filePath string) (string, error) {
// 加载系统dll
versionDLL := syscall.NewLazyDLL("version.dll")
getFileVersionInfoSize := versionDLL.NewProc("GetFileVersionInfoSizeW")
Expand Down Expand Up @@ -77,5 +78,6 @@ func GetExeDllVersion(filePath string) (string, error) {
minor := fixedInfo.FileVersionMS & 0xFFFF
build := fixedInfo.FileVersionLS >> 16
revision := fixedInfo.FileVersionLS & 0xFFFF

return fmt.Sprintf("%d.%d.%d.%d", major, minor, build, revision), nil
}
4 changes: 2 additions & 2 deletions fileutil/file_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ package fileutil

import "testing"

func TestGetExeDllVersion(t *testing.T) {
v, err := GetExeDllVersion(`C:\Windows\System32\cmd.exe`)
func TestGetExeOrDllVersion(t *testing.T) {
v, err := GetExeOrDllVersion(`C:\Windows\System32\cmd.exe`)
if err != nil {
t.Error(err)
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ go 1.18

require (
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a
golang.org/x/sys v0.5.0
golang.org/x/text v0.9.0
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a h1:4iLhBPcpqFmylhnkbY3W0ONLUYYkDAW9xMFLfxgsvCw=
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
7 changes: 4 additions & 3 deletions random/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,12 @@ func UUIdV4() (string, error) {
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
}

// RandNumLen 生成一个长度为len的随机数
// Play: https://go.dev/play/p/jpIalcD5rMo
func RandNumLen(len int) int {
// RandNumberOfLength 生成一个长度为len的随机数
// Play: todo
func RandNumberOfLength(len int) int {
m := int(math.Pow10(len) - 1)
i := int(math.Pow10(len - 1))
ret := rand.Intn(m-i+1) + i

return ret
}
6 changes: 3 additions & 3 deletions random/random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,9 @@ func TestRandBoolSlice(t *testing.T) {
}
})
}
func TestRandNumLen(t *testing.T) {
func TestRandNumberOfLength(t *testing.T) {
t.Parallel()
randi := RandNumLen(6)
assert := internal.NewAssert(t, "TestRandNumLen")
randi := RandNumberOfLength(6)
assert := internal.NewAssert(t, "TestRandNumberOfLength")
assert.Equal(6, len(strconv.Itoa(randi)))
}

0 comments on commit 0a2cc9c

Please sign in to comment.