diff --git a/go.mod b/go.mod index faaa43e..a4b4065 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,8 @@ require ( github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 github.com/go-ini/ini v1.67.0 github.com/mitchellh/go-homedir v1.1.0 + github.com/mozillazg/go-pinyin v0.20.0 + github.com/mssola/user_agent v0.6.0 github.com/pkg/errors v0.9.1 github.com/spaolacci/murmur3 v1.1.0 github.com/stretchr/testify v1.8.2 diff --git a/go.sum b/go.sum index def97e5..da9d2ca 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,10 @@ github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ= +github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc= +github.com/mssola/user_agent v0.6.0 h1:uwPR4rtWlCHRFyyP9u2KOV0u8iQXmS7Z7feTrstQwk4= +github.com/mssola/user_agent v0.6.0/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/init.go b/init.go index 3455b98..68b7fd9 100644 --- a/init.go +++ b/init.go @@ -42,6 +42,10 @@ var ( Console *console // Browser 浏览器操作实例 Browser *browser + // StringConvert 字符串转为任意类型 + StringConvert *stringConvert + // PinYin 汉字转拼音 + PinYin *pinYin ) func init() { @@ -70,4 +74,7 @@ func init() { "linux": "xdg-open", }, } + // StringConvert + StringConvert = &stringConvert{} + PinYin = &pinYin{} } diff --git a/json.go b/json.go index c360d40..26e8fdb 100644 --- a/json.go +++ b/json.go @@ -46,6 +46,35 @@ func (oj *ownJSON) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiv return decoder.Decode(receiver) } +// UnmarshalWithNumberForString 字符串转结构体 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:50 2023/7/22 +func (oj *ownJSON) UnmarshalWithNumberForString(input string, receiver interface{}) error { + return oj.UnmarshalWithNumber([]byte(input), receiver) +} + +// MarshalForByte 序列化并返回字节数组 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:56 2023/7/22 +func (oj *ownJSON) MarshalForByte(input interface{}) []byte { + byteData, _ := json.Marshal(input) + return byteData +} + +// MarshalForString 序列化并返回字符串 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:56 2023/7/22 +func (oj *ownJSON) MarshalForString(input interface{}) string { + byteData := oj.MarshalForByte(input) + return string(byteData) +} + // ConsoleOutput ... // // Author : go_developer@163.com<白茶清欢> diff --git a/map.go b/map.go index 0c65279..d60d5ed 100644 --- a/map.go +++ b/map.go @@ -214,3 +214,16 @@ func (om *ownMap) GetDataWithReceiver(source map[interface{}]interface{}, key in } return ConvertAssign(receiver, source[key]) } + +// ToStruct map转结构体 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:48 2023/7/22 +func (om *ownMap) ToStruct(source map[string]interface{}, receiver interface{}) error { + if nil == source { + return nil + } + byteData := JSON.MarshalForByte(source) + return JSON.UnmarshalWithNumber(byteData, receiver) +} diff --git a/string.go b/string.go index de97f97..dd76bcb 100644 --- a/string.go +++ b/string.go @@ -17,6 +17,14 @@ import ( "github.com/axgle/mahonia" ) +var ( + // LETTER_LIST 字母列表 + LETTER_LIST = []string{ + "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", + "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", + } +) + // stringOperate ... // // Author : go_developer@163.com<白茶清欢> diff --git a/string_convert.go b/string_convert.go new file mode 100644 index 0000000..3ac30ee --- /dev/null +++ b/string_convert.go @@ -0,0 +1,653 @@ +// Package util ... +// +// Description : util ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-05-04 18:25 +package util + +import ( + "strings" + + "github.com/pkg/errors" +) + +type stringConvert struct { +} + +// ToFloat32 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:26 2023/5/4 +func (sc *stringConvert) ToFloat32(str string) (float32, error) { + var ( + err error + res float32 + ) + err = ConvertAssign(&res, str) + return res, err +} + +// ToFloat64 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:26 2023/5/4 +func (sc *stringConvert) ToFloat64(str string) (float64, error) { + var ( + err error + res float64 + ) + err = ConvertAssign(&res, str) + return res, err +} + +// ToDouble 转double +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:33 2023/5/4 +func (sc *stringConvert) ToDouble(str string) (float64, error) { + return sc.ToFloat64(str) +} + +// ToNumber 转数字, 使用最高精度的float64 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:33 2023/5/4 +func (sc *stringConvert) ToNumber(str string) (float64, error) { + return sc.ToFloat64(str) +} + +// ToInt8 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToInt8(str string) (int8, error) { + var ( + err error + res int8 + ) + err = ConvertAssign(&res, str) + return res, err +} + +// ToInt16 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToInt16(str string) (int16, error) { + var ( + err error + res int16 + ) + err = ConvertAssign(&res, str) + return res, err +} + +// ToInt32 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToInt32(str string) (int32, error) { + var ( + err error + res int32 + ) + err = ConvertAssign(&res, str) + return res, err +} + +// ToInt64 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToInt64(str string) (int64, error) { + var ( + err error + res int64 + ) + err = ConvertAssign(&res, str) + return res, err +} + +// ToInt ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:47 2023/5/5 +func (sc *stringConvert) ToInt(str string) (int, error) { + var ( + err error + res int + ) + err = ConvertAssign(&res, str) + return res, err +} + +// ToUint8 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:30 2023/5/4 +func (sc *stringConvert) ToUint8(str string) (uint8, error) { + var ( + err error + res uint8 + ) + err = ConvertAssign(&res, str) + return res, err +} + +// ToUint16 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToUint16(str string) (uint16, error) { + var ( + err error + res uint16 + ) + err = ConvertAssign(&res, str) + return res, err +} + +// ToUint32 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToUint32(str string) (uint32, error) { + var ( + err error + res uint32 + ) + err = ConvertAssign(&res, str) + return res, err +} + +// ToUint64 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToUint64(str string) (uint64, error) { + var ( + err error + res uint64 + ) + err = ConvertAssign(&res, str) + return res, err +} + +// ToUint ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToUint(str string) (uint, error) { + var ( + err error + res uint + ) + err = ConvertAssign(&res, str) + return res, err +} + +// ToBool ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:34 2023/5/4 +func (sc *stringConvert) ToBool(str string) (bool, error) { + var ( + err error + res bool + ) + err = ConvertAssign(&res, str) + return res, err +} + +// ToObject ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:35 2023/5/4 +func (sc *stringConvert) ToObject(str string) (map[string]interface{}, error) { + var ( + err error + result map[string]interface{} + ) + if err = JSON.UnmarshalWithNumber([]byte(str), &result); nil != err { + return nil, err + } + return result, nil +} + +// ToStruct ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:38 2023/5/4 +func (sc *stringConvert) ToStruct(str string, receiver interface{}) error { + if nil == receiver { + return errors.New("receiver is nil") + } + return JSON.UnmarshalWithNumber([]byte(str), receiver) +} + +// ToInt8Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:55 2023/5/4 +func (sc *stringConvert) ToInt8Slice(str string, splitChar ...string) ([]int8, error) { + var ( + err error + res []int8 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]int8, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToInt8(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToInt16Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:01 2023/5/4 +func (sc *stringConvert) ToInt16Slice(str string, splitChar ...string) ([]int16, error) { + var ( + err error + res []int16 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]int16, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToInt16(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToInt32Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:03 2023/5/4 +func (sc *stringConvert) ToInt32Slice(str string, splitChar ...string) ([]int32, error) { + var ( + err error + res []int32 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]int32, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToInt32(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToInt64Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:04 2023/5/4 +func (sc *stringConvert) ToInt64Slice(str string, splitChar ...string) ([]int64, error) { + var ( + err error + res []int64 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]int64, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToInt64(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToIntSlice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:04 2023/5/4 +func (sc *stringConvert) ToIntSlice(str string, splitChar ...string) ([]int, error) { + var ( + err error + res []int + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]int, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToInt(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToUint8Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:50 2023/5/5 +func (sc *stringConvert) ToUint8Slice(str string, splitChar ...string) ([]uint8, error) { + var ( + err error + res []uint8 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]uint8, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToUint8(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToUint16Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:52 2023/5/5 +func (sc *stringConvert) ToUint16Slice(str string, splitChar ...string) ([]uint16, error) { + var ( + err error + res []uint16 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]uint16, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToUint16(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToUint32Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:53 2023/5/5 +func (sc *stringConvert) ToUint32Slice(str string, splitChar ...string) ([]uint32, error) { + var ( + err error + res []uint32 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]uint32, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToUint32(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToUint64Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:54 2023/5/5 +func (sc *stringConvert) ToUint64Slice(str string, splitChar ...string) ([]uint64, error) { + var ( + err error + res []uint64 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]uint64, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToUint64(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToUintSlice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:54 2023/5/5 +func (sc *stringConvert) ToUintSlice(str string, splitChar ...string) ([]uint, error) { + var ( + err error + res []uint + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]uint, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToUint(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToBoolSlice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:55 2023/5/5 +func (sc *stringConvert) ToBoolSlice(str string, splitChar ...string) ([]bool, error) { + var ( + err error + res []bool + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]bool, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToBool(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToFloat32Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:56 2023/5/5 +func (sc *stringConvert) ToFloat32Slice(str string, splitChar ...string) ([]float32, error) { + var ( + err error + res []float32 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]float32, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToFloat32(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToFloat64Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:56 2023/5/5 +func (sc *stringConvert) ToFloat64Slice(str string, splitChar ...string) ([]float64, error) { + var ( + err error + res []float64 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]float64, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToFloat64(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToDoubleSlice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:57 2023/5/5 +func (sc *stringConvert) ToDoubleSlice(str string, splitChar ...string) ([]float64, error) { + return sc.ToFloat64Slice(str, splitChar...) +} + +// ToNumberSlice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:57 2023/5/5 +func (sc *stringConvert) ToNumberSlice(str string, splitChar ...string) ([]float64, error) { + return sc.ToFloat64Slice(str, splitChar...) +} diff --git a/user_agent.go b/user_agent.go index c5aebf9..4873f89 100644 --- a/user_agent.go +++ b/user_agent.go @@ -10,6 +10,8 @@ package util import ( "math/rand" "time" + + "github.com/mssola/user_agent" ) var defaultUserAgentList = []string{ @@ -98,3 +100,77 @@ func (ua *userAgent) GetRandomUA() string { idx := rand.Intn(len(ua.list)) return ua.list[idx] } + +// Parse 解析UA +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:31 2023/4/20 +func (ua *userAgent) Parse(inputUA string) *UAInfo { + uaInstance := user_agent.New(inputUA) + uaInfo := &UAInfo{ + Mozilla: uaInstance.Mozilla(), + Platform: uaInstance.Platform(), + OS: uaInstance.OS(), + Localization: uaInstance.Localization(), + Model: uaInstance.Model(), + Browser: &BrowserInfo{ + Engine: "", + EngineVersion: "", + Name: "", + Version: "", + }, + OSInfo: &OSInfo{ + FullName: uaInstance.OSInfo().FullName, + Name: uaInstance.OSInfo().Name, + Version: uaInstance.OSInfo().Version, + }, + Bot: false, + Mobile: false, + Undecided: false, + } + uaInfo.Browser.Engine, uaInfo.Browser.EngineVersion = uaInstance.Engine() + uaInfo.Browser.Name, uaInfo.Browser.Version = uaInstance.Browser() + return uaInfo +} + +// UAInfo ua解析结果 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:34 2023/4/20 +type UAInfo struct { + Mozilla string `json:"mozilla"` + Platform string `json:"platform"` + OS string `json:"os"` + OSInfo *OSInfo `json:"os_info"` + Localization string `json:"localization"` + Model string `json:"model"` + Browser *BrowserInfo `json:"browser"` + Bot bool `json:"bot"` + Mobile bool `json:"mobile"` + Undecided bool `json:"undecided"` +} + +// BrowserInfo 浏览器信息 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:34 2023/4/20 +type BrowserInfo struct { + Engine string `json:"engine"` // 浏览器引擎 + EngineVersion string `json:"engine_version"` // 浏览器引擎版本 + Name string `json:"name"` // 浏览器名称 + Version string `json:"version"` // 浏览器版本 +} + +// OSInfo 系统信息 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:34 2023/4/20 +type OSInfo struct { + FullName string `json:"full_name"` // 操作系统全称 + Name string `json:"name"` // 操作系统名称 + Version string `json:"version"` // 操作系统版本 +} diff --git a/util.go b/util.go new file mode 100644 index 0000000..4c08359 --- /dev/null +++ b/util.go @@ -0,0 +1,208 @@ +// Package util ... +// +// Description : util ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-06-25 17:22 +package util + +import "github.com/mozillazg/go-pinyin" + +// PinYinArg 设置选项方法 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:33 2023/6/25 +type PinYinArg func(arg *pinyin.Args) + +// 拼音风格的设置 + +// PinYinStyleNormal ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:37 2023/6/25 +func PinYinStyleNormal() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.Normal + } +} + +// PinYinStyleToneStand 声调风格1,拼音声调在韵母第一个字母上。如: zhōng guó +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:38 2023/6/25 +func PinYinStyleToneStand() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.Tone + } +} + +// PinYinStyleToneAfterYunMu 声调风格2,即拼音声调在各个韵母之后,用数字 [1-4] 进行表示。如: zho1ng guo2 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:39 2023/6/25 +func PinYinStyleToneAfterYunMu() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.Tone2 + } +} + +// PinYinStyleToneAtEnding 声调风格3,即拼音声调在各个拼音之后,用数字 [1-4] 进行表示。如: zhong1 guo2 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:40 2023/6/25 +func PinYinStyleToneAtEnding() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.Tone3 + } +} + +// PinYinStyleShengMu 声母风格,只返回各个拼音的声母部分。如: zh g 。注意:不是所有的拼音都有声母 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:41 2023/6/25 +func PinYinStyleShengMu() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.Initials + } +} + +// PinYinStyleFirstLetter 首字母风格,只返回拼音的首字母部分。如: z g +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:42 2023/6/25 +func PinYinStyleFirstLetter() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.FirstLetter + } +} + +// PinYinStyleYunMu 韵母风格,只返回各个拼音的韵母部分,不带声调。如: ong uo +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:43 2023/6/25 +func PinYinStyleYunMu() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.Finals + } +} + +// PinYinStyleToneYunMu 韵母风格1,带声调,声调在韵母第一个字母上。如: ōng uó +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:44 2023/6/25 +func PinYinStyleToneYunMu() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.FinalsTone + } +} + +// PinYinStyleToneYunMuAtStart 韵母风格2,带声调,声调在各个韵母之后,用数字 [1-4] 进行表示。如: o1ng uo2 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:45 2023/6/25 +func PinYinStyleToneYunMuAtStart() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.FinalsTone2 + } +} + +// PinYinStyleToneYunMuAtEnding 韵母风格3,带声调,声调在各个拼音之后,用数字 [1-4] 进行表示。如: ong1 uo2 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:40 2023/6/25 +func PinYinStyleToneYunMuAtEnding() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.FinalsTone3 + } +} + +// 拼音风格设置结束 + +// PinYinStyleWithSeparator 设置拼音的分隔符 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:59 2023/6/25 +func PinYinStyleWithSeparator(separator string) PinYinArg { + return func(arg *pinyin.Args) { + if len(separator) == 0 { + return + } + arg.Separator = separator + } +} + +// PinYinStyleWithHeteronym 开启多音字模式 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:03 2023/6/25 +func PinYinStyleWithHeteronym() PinYinArg { + return func(arg *pinyin.Args) { + arg.Heteronym = true + } +} + +// PinYin 汉字转拼音 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:23 2023/6/25 +type pinYin struct { +} + +// Convert 汉字生成拼音 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:26 2023/6/25 +func (py *pinYin) Convert(text string, argFuncList ...PinYinArg) string { + arg := pinyin.NewArgs() + arg.Separator = " " + for _, argFunc := range argFuncList { + argFunc(&arg) + } + + return pinyin.Slug(text, arg) +} + +// ConvertSingle 每个字一个读音, 支持多音字 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:02 2023/6/25 +func (py *pinYin) ConvertSingle(text string, argFuncList ...PinYinArg) []string { + arg := pinyin.NewArgs() + arg.Separator = " " + for _, argFunc := range argFuncList { + argFunc(&arg) + } + + return pinyin.LazyPinyin(text, arg) +} + +// ConvertWithHeteronym 多音字模式 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:02 2023/6/25 +func (py *pinYin) ConvertWithHeteronym(text string, argFuncList ...PinYinArg) [][]string { + arg := pinyin.NewArgs() + arg.Separator = " " + for _, argFunc := range argFuncList { + argFunc(&arg) + } + return pinyin.Pinyin(text, arg) +}