1 Star 0 Fork 14

firecream / go-agi

forked from K. / go-agi 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Go语言工具辅助库

Go语言是一门伟大、强大的语言,我接触他只有一个多月的时间,已经深刻感受到了他的魅力。

不得不说,Go仍是一门处于发展阶段的语言,尤其是他也是游走于面向函数、面向对象和面向接口编程的边缘(和Scala类似,不过Scala的类和Trait的数量实在让人望而却步)。也包括Go本身提供的很多类库,都存在大量的枚举、反射、hardcode的问题。

所以有了这个工具辅助库,这个库的设计初衷是面向使用,而非做一个大全、或高性能(毕竟Go已经提供了一个环境),我最主要的一个目的就是,将一些日常频繁使用的方法,能用一个统一明确的方式返回结果,少写两个if。

获取指令:go get git.oschina.net/janpoem/go-agi

工具库

agi.SplitDirFile(path string) (string, string)

根据传入的路径,分离出目录和文件

agi.RealPathSplitDirFile(path string) (string, string, error)

基于真实的物理路径,检查传入的路径,并分离出目录和文件名,这个函数还有待调整。

agi.Exist(path string) bool

检查路径是否存在,这个不会区分是文件还是目录。

agi.IsFile(path string) bool

检查路径是不是文件

agi.IsDir(path string) bool

检查路径是不是目录

agi.ReadFileByte(path string) ([]byte, error)

读取指定的文件,并返回[]byte

agi.ReadFileStr(path string) (string, error)

读取指定的文件,并返回字符串

agi.TF(format string) (string)

生成go的时间格式,主要基于PHP的时间格式,目前支持:Y-m-d H:i:sY-n-j H:i:s

agi.OffsetIndex(index int, size int) int

取得索引值的偏移值,-1对应的是size的最末。

agi.Seconds(seconds int) time.Duration

取得多少秒的间隔时间

agi.Minutes(minutes int) time.Duration

取得多少分钟的间隔时间

agi.Hours(hours int) time.Duration

取得多少小时的间隔时间

性能测试

这部分以后会改为一个Benchmark struct。性能测试的代码,主要为了方便对一些写法的性能能快速的做一个甄别,以后会考虑加入CPU和内存的监控。

agi.TimesTest(times int, fn func(int)) time.Duration

循环执行指定的匿名函数次数,并返回执行过程所花费的时间。

运行时

这里主要提供一些识别运行时的环境变量,并且这些环境变量不应该可变,所以采用函数的方式来查询。

agi.Args() []string

返回执行当前文件的命令行参数,注意不包含可执行文件本身。这里会后面会加入start-stop-daemon的那个--的判断。

agi.AppPath() string

返回当前执行文件的全路径

agi.AppDir() string

返回当前执行文件的目录

agi.AppFile() string

返回当前执行文件的文件名

agi.AppTimeZone() string

返回当前运行时的时区名称,注意,凡是涉及当前运行时的时区,偏移值的,都是基于启动时,创建一个time.Now(),需要确保你执行环境的系统已经设置了正确的时区(服务器上手第一修正时区,第二upgrade)。

agi.AppTimeOffset() int

取得当前时区的日期偏移值

agi.AppTimeLoc() *time.Location

返回当前时区的Location对象,如果你输出这个对象会不显示任何值(都是私有值),在很多修正时间的地方需要用到。

agi.GetOsFlag() int

取得系统的标示整型OS_LINUX => 0, OS_MAC => 1, OS_WIN => 2, OS_OTHERS => 3。

agi.GetOsEof() string

取得系统的EOF

agi.GetOsDirSpr() string

取得系统目录的分隔符。

agi.GetOsEndian() binary.ByteOrder

取得系统的编码顺序,大小编码,你懂的。

类型处理

agi.BytesToStrUnsafe(rawBytes []byte) string

[]byte转字符串类型,据说是黑魔法

agi.Round(f float64, places int) string

浮点数取精确的小数点数

agi.StrToFloat(value string) float64

字符串数值转浮点,返回的是float64。如果为非数值字符,返回0

agi.StrToFInt(value string) int

字符串数值转整型,不过会先转为浮点,而后再转为整型,尽可能的取得一个有效的值。如果为非数值字符,返回0

agi.StrToFInt64(value string) int64

字符串数值转64位整型,同上

agi.StrToInt(value string) int

这个只是单纯意义的转整型。

agi.StrToIntBool(value string) bool

字符串整值转为布尔类型,这里特别针对0 => false, 大于 0 => true,而不同于go本身提供的ParseBoolean

agi.StrToTime(value string, layout string) time.Time

字符串的时间值,转为时间类型,layout这里是输入go那个奇葩的时间格式:Mon Jan 2 15:04:05 -0700 MST 2006。

为了方便使用,目前提供一个函数agi.TF("Y-m-d H:i:s")的方式转为go的时间格式(PHP风格),不过小弟时间有限,没有支持太多的format。

整体调用时:agi.StrToTime("2014-12-31 12:33:34", agi.TF("Y-m-d H:i:s"))

agi.IntToTime(value int64) time.Time

字符串的数值转为时间类型,特别注意,如果传入的值 <= 0,也会返回一个时间值,但这个时间类型的值得是无效的。要判断其是否为有效的时间值,需要使用agi.IsValidTime(agi.IntToTime(value))

agi.IsValidTime(t time.Time) bool

检查传入的时间值是不是一个有效的时间值。

agi.CallAnyStructMethod(v interface{}, method string) interface{}

调用任意结构的任意的方法,这个函数还需要修改,需要允许传参数,和返回[]interface{}

agi.KindOf(v interface {}) reflect.Kind

取得任意变量的go原生类型(值反射)。注意,这里获取的是go的原生类型,这是个非常非常重要的方法。如果传入的变量是一个指针,会直接去取指针指向的实际变量。

比如有下面的代码:

type XYZ struct {
}

type MyInt int

var mya MyInt = 1

agi.KindOf(&XYZ{}) // struct,用于程序等式判断的话,是reflect.Struct
agi.KindOf(mya) // int,用于程序等式判断的话,是reflect.Int
agi.ValueOf(v interface {}) reflect.Value

取得任意变量的实际值(值反射),和KindOf一样,如果是一个指针,会去取指针实际指向的变量。

agi.ValueOfagi.KindOfagi.CallAnyStructMethod是三个杀手级的方法,go的反射其实蛮完善的,就是……用起来比较费劲,有了这三个函数,能助你立刻去掉那些纷繁的东西,迅速掌握go的反射。让你用起Go语言,比用PHP还简单。

agi.AnyToInt64(v interface{}) int64
agi.AnyToInt(v interface{}) int
agi.AnyToInt8(v interface{}) int8
agi.AnyToInt16(v interface{}) int16
agi.AnyToUInt8(v interface{}) uint8
agi.AnyToUInt16(v interface{}) uint16
agi.AnyToUInt64(v interface{}) uint64

任意值类型,转为整型,目标就是要能涵盖目前go可能出现的变量类型。并且支持,如果是一个结构,尝试检索对象是否具有一个叫做Int的方法,并返回该函数的结果。

agi.AnyToBool(v interface{}) bool

任意值类型转布尔类型,如果传入的是数组、切片、map的话,会检查他们的长度是否大于0,大于0就返回true。

agi.AnyToStr(v interface{}) string

任意值类型转字符串类型,如果是一个结构,会尝试执行结构的String的方法。

agi.AnyToFloat(v interface{}) float64

任意值类型转float64类型,如果是一个结构,会尝试执行Float的方法。

agi.AnyToRound(v interface{}, places int) float64

任意值类型,Round方法,会先转浮点,而后执行Round。

agi.AnyToTime(v interface{}) time.Time

任意值类型,转时间类型,如果是一个结构,会先尝试执行结构的Time方法。

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: You must give any other recipients of the Work or Derivative Works a copy of this License; and You must cause any modified files to carry prominent notices stating that You changed the files; and You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2015 曾建凯 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

Go语言工具库,面向使用,让Go编码更轻松! 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/firecream/go-agi.git
git@gitee.com:firecream/go-agi.git
firecream
go-agi
go-agi
master

搜索帮助

14c37bed 8189591 565d56ea 8189591