Gox语言不仅包含了大部分Go语言的标准库对象,以及很多常用的第三方库,还提供了很多简洁实用的内置函数。常用的内置函数包括:
最新内置函数清单(左栏引号内的是)
// 其中 tk.开头的函数都是github.com/topxeq/tk包中的,可以去pkg.go.dev/github.com/topxeq/tk查看函数定义
// common related 一般函数
"defined": defined, // 查看某变量是否已经定义,注意参数是字符串类型的变量名,例: if defined("a") {...}
"pass": tk.Pass, // 没有任何操作的函数,一般用于脚本结尾避免脚本返回一个结果导致输出乱了
"isDefined": isDefined, // 判断某变量是否已经定义,与defined的区别是传递的是变量名而不是字符串方式的变量,例: if isDefined(a) {...}
"isDef": isDefined, // 等同于isDef
"isUndefined": isUndefined, // 判断某变量是否未定义
"isUndef": isUndefined, // 等同于isUndefined
"isNil": isNil, // 判断一个变量或表达式是否为nil
"isValid": isValid, // 判断某变量是否已经定义,并且不是nil,如果传入了第二个参数,还可以判断该变量是否类型是该类型,例: if isValid(a, "string") {...}
"isValidNotEmpty": isValidNotEmpty, // 判断某变量是否已经定义,并且不是nil或空字符串,如果传入了第二个参数,还可以判断该变量是否类型是该类型,例: if isValid(a, "string") {...}
"isValidX": isValidNotEmpty, // 等同于isValidNotEmpty
"eval": qlEval, // 运行一段Gox语言代码并获得其返回值,返回值可以放于名为outG的全局变量中,也可以作为最后一个表达式的返回值返回
"typeOf": tk.TypeOfValue, // 给出某变量的类型名
"typeOfReflect": tk.TypeOfValueReflect, // 给出某变量的类型名(使用了反射方式)
"typeOfVar": typeOfVar, // 给出某变量的内部类型名,注意参数是字符串类型的变量名
"exit": tk.Exit, // 立即退出脚本的执行,可以带一个整数作为参数,也可以没有
"setValue": tk.SetValue, // 用反射的方式设定一个变量的值
"getValue": tk.GetValue, // 用反射的方式获取一个变量的值
"getPointer": tk.GetPointer, // 用反射的方式获取一个变量的指针
"getAddr": tk.GetAddr, // 用反射的方式获取一个变量的地址
"setVar": tk.SetVar, // 设置一个全局变量,例: setVar("a", "value of a")
"getVar": tk.GetVar, // 获取一个全局变量的值,例: v = getVar("a")
"ifThenElse": tk.IfThenElse, // 相当于三元操作符a?b:c,但注意a、b、c三个表达式仍需语法正确
"ifElse": tk.IfThenElse, // 相当于ifThenElse
"ifThen": tk.IfThenElse, // 相当于ifThenElse
"deepClone": tk.DeepClone,
"deepCopy": tk.DeepCopyFromTo,
"run": runFile,
"runCode": runCode,
"runScript": runScript,
"magic": magic,
// debug relate 调试相关
"dump": tk.Dump, // 输出一个或多个对象信息供参考
"dumpf": tk.Dumpf,
"sdump": tk.Sdump, // 生成一个或多个对象信息供参考
"sdumpf": tk.Sdumpf,
// output related 输出相关
"pv": printValue, // 输出一个变量的值,注意参数是字符串类型的变量名,例: pv("a")
"pr": tk.Pr, // 等同于其他语言中的print
"prf": tk.Printf, // 等同于其他语言中的printf
"pln": tk.Pln, // 等同于其他语言中的println
"printfln": tk.Pl, // 等同于其他语言中的printf,但多输出一个回车换行
"pl": tk.Pl, // 等同于printfln
"sprintf": fmt.Sprintf, // 等同于其他语言中的sprintf
"spr": fmt.Sprintf, // 等同于sprintf
"fprintf": fmt.Fprintf, // 等同于其他语言中的frintf
"plv": tk.Plv, // 输出某变量或表达式的内容/值,以Go语言内部的表达方式,例如字符串将加上双引号
"plvx": tk.Plvx, // 输出某变量或表达式的内容/值和类型等信息
"plNow": tk.PlNow, // 相当于pl,但前面多加了一个时间标记
"plVerbose": tk.PlVerbose, // 相当于pl,但前面多了一个布尔类型的参数,可以传入一个verbose变量,指定是否输出该信息,例:
// v = false
// plVerbose(v, "a: %v", 3) // 由于v的值为false,因此本条语句将不输出
"vpl": tk.PlVerbose, // 等同于plVerbose
"plvsr": tk.Plvsr, // 输出多个变量或表达式的值,每行一个
"plerr": tk.PlErr, // 快捷输出一个error类型的值
"plExit": tk.PlAndExit, // 相当于pl然后exit退出脚本的执行
// input related 输入相关
"getChar": tk.GetChar, // 从命令行获取用户的输入,成功返回一个表示字符字符串(控制字符代码+字符代码),否则返回error对象
"getChar2": tk.GetChar2, // 从命令行获取用户的输入,成功返回一个表示字符ASCII码的字符串,否则返回error对象
"getInput": tk.GetUserInput, // 从命令行获取用户的输入
"getInputf": tk.GetInputf, // 从命令行获取用户的输入,同时可以用printf先输出一个提示信息
"getPasswordf": tk.GetInputPasswordf, // 从命令行获取密码输入,输入信息将不显示
// math related数学相关
"bitXor": tk.BitXor, // 异或运算
"adjustFloat": tk.AdjustFloat, // 去除浮点数的计算误差,用法:adjustFloat(4.000000002, 2),第二个参数表示保留几位小数点后数字
"getRandomInt": tk.GetRandomIntLessThan, // 获取[0-maxA)之间的随机整数
"getRandom": tk.GetRandomFloat, // 获取[0.0-1.0)之间的随机浮点数
// string related 字符串相关
"trim": trim, // 取出字符串前后的空白字符,可选的第二个参数可以是待去掉的字符列表,等同于tk.Trim, 但支持Undefind(转空字符串)和nil
"strTrim": tk.Trim, // 等同于tk.Trim
"trimSafely": tk.TrimSafely, // 取出字符串前后的空白字符,非字符串则返回默认值空,可以通过第二个(可选)参数设置默认值
"trimx": tk.TrimSafely, // 等同于trimSafely
"trimX": tk.TrimSafely, // 等同于trimSafely
"toLower": strings.ToLower, // 字符串转小写
"toUpper": strings.ToUpper, // 字符串转大写
"padStr": tk.PadString, // 字符串补零等填充操作,例如 s1 = padStr(s0, 5, "-fill=0", "-right=true"),第二个参数是要补齐到几位,默认填充字符串fill为字符串0,right(表示是否在右侧填充)为false(也可以直接写成-right),因此上例等同于padStr(s0, 5),如果fill字符串不止一个字符,最终补齐数量不会多于第二个参数指定的值,但有可能少
"limitStr": tk.LimitString, // 超长字符串截短,用法 s2 = limitStr("abcdefg", 3, "-suffix=..."),将得到abc...,suffix默认为...
"strContains": strings.Contains, // 判断字符串中是否包含某个字串
"strContainsIn": tk.ContainsIn, // 判断字符串中是否包含某几个字串
"strReplace": tk.Replace, // 替换字符串中的字串
"strReplaceIn": tk.StringReplace, // strReplaceIn("2020-02-02 08:09:15", "-", "", ":", "", " ", "")
"strJoin": strJoin, // 连接一个字符串数组,以指定的分隔符,例: s = strJoin(listT, "\n")
"strSplit": strings.Split, // 拆分一个字符串为数组,例: listT = strSplit(strT, "\n")
"strSplitByLen": tk.SplitByLen, // 按长度拆分一个字符串为数组,注意由于是rune,可能不是按字节长度,例: listT = strSplitByLen(strT, 10),可以加第三个参数表示字节数不能超过多少,加第四个参数表示分隔符(遇上分隔符从分隔符后重新计算长度,也就是说分割长度可以超过指定的个数,一般用于有回车的情况)
"splitLines": tk.SplitLines, // 相当于strSplit(strT, "\n")
"strSplitLines": tk.SplitLines, // 相当于splitLines
"startsWith": tk.StartsWith, // 判断字符串是否以某子串开头
"strStartsWith": tk.StartsWith, // 等同于startsWith
"endsWith": tk.EndsWith, // 判断字符串是否以某子串结尾
"strEndsWith": tk.EndsWith, // 等同于endsWith
"strIn": tk.InStrings, // 判断字符串是否在一个字符串列表中出现,函数定义: strIn(strA string, argsA ...string) bool,第一个可变参数如果以“-”开头,将表示参数开关,-it表示忽略大小写,并且trim再比较(strA并不trim)
"strFindAll": tk.FindSubStringAll, // 寻找字符串中某个子串出现的所有位置,函数定义: func strFindAll(strA string, subStrA string) [][]int,每个匹配是两个整数,分别表示开头和结尾(不包含)
"newStringBuilder": newStringBuilder, // 新建一个strings.Builder对象
"newStringBuffer": newStringBuilder, // 同newStringBuilder
"getNowStr": tk.GetNowTimeStringFormal, // 获取一个表示当前时间的字符串,格式:2020-02-02 08:09:15
"getNowString": tk.GetNowTimeStringFormal, // 等同于getNowStr
"getNowStrCompact": tk.GetNowTimeString, // 获取一个简化的表示当前时间的字符串,格式:20200202080915
"getNowStringCompact": tk.GetNowTimeStringFormal, // 等同于getNowStringCompact
"getNowDateStrCompact": getNowDateStrCompact, // 获取一个简化的表示当前日期的字符串,格式:20210215
"genTimeStamp": tk.GetTimeStampMid, // 生成时间戳,格式为13位的Unix时间戳1640133706954,例:timeStampT = genTimeStamp(time.Now())
"genRandomStr": tk.GenerateRandomStringX, // 生成随机字符串,函数定义: genRandomStr("-min=6", "-max=8", "-noUpper", "-noLower", "-noDigit", "-special", "-space", "-invalid")
"generateRandomString": tk.GenerateRandomString, // 生成随机字符串,函数定义: (minCharA, maxCharA int, hasUpperA, hasLowerA, hasDigitA, hasSpecialCharA, hasSpaceA bool, hasInvalidChars bool) string
// regex related 正则表达式相关
"regMatch": tk.RegMatchX, // 判断某字符串是否完整符合某表达式,例: if regMatch(mailT, `^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$`) {...}
"regContains": tk.RegContainsX, // 判断某字符串是否包含符合正则表达式的子串,例: if regContains("abccd", "b.c") {...}
"regContainsIn": tk.RegContainsIn, // 判断字符串中是否包含符合正则表达式的某几个字串
"regCount": tk.RegCount, // 判断某字符串包含几个符合正则表达式的子串
"regFind": tk.RegFindFirstX, // 根据正则表达式在字符串中寻找第一个匹配,函数定义: func regFind(strA, patternA string, groupA int) string
"regFindAll": tk.RegFindAllX, // 根据正则表达式在字符串中寻找所有匹配,函数定义: func regFindAll(strA, patternA string, groupA int) []string
"regFindIndex": tk.RegFindFirstIndexX, // 根据正则表达式在字符串中第一个匹配的为止,函数定义: func regFindIndex(strA, patternA string) (int, int)
"regFindAllIndex": tk.RegFindAllIndexX, // 根据正则表达式搜索在字符串中所有匹配,函数定义: func regFindAllIndex(strA, patternA string) [][]int
"regReplace": tk.RegReplaceX, // 根据正则表达式在字符串中进行替换,函数定义: regReplace(strA, patternA, replaceA string) string, 例:regReplace("abcdefgabcdfg", "(b.*)f(ga.*?)g", "${1}_${2}"),结果是abcde_gabcdf
"regSplit": tk.RegSplitX, // 根据正则表达式分割字符串(以符合条件的匹配来分割),函数定义: regSplit(strA, patternA string, nA ...int) []string
// conversion related 转换相关
"nilToEmpty": nilToEmpty, // 将nil、error等值都转换为空字符串,其他的转换为字符串, 加-nofloat参数将浮点数转换为整数,-trim参数将结果trim
"nilToEmptyOk": nilToEmptyOk, // 将nil、error等值都转换为空字符串,其他的转换为字符串, 加-nofloat参数将浮点数转换为整数,-trim参数将结果trim,第二个返回值是bool类型,如果值是undefined,则返回false,其他情况为true
"intToStr": tk.IntToStrX, // 整数转字符串
"strToInt": tk.StrToIntWithDefaultValue, // 字符串转整数
"floatToStr": tk.Float64ToStr, // 浮点数转字符串
"strToFloat": tk.StrToFloat64, // 字符串转浮点数,如果第二个参数(可选)存在,则默认错误时返回该值,否则错误时返回-1
"timeToStr": tk.FormatTime, // 时间转字符串,函数定义: timeToStr(timeA time.Time, formatA ...string) string,formatA可为"2006-01-02 15:04:05"(默认值)等字符串,为compact代表“20060102150405”
"timeStampToTime": tk.GetTimeFromUnixTimeStampMid, // Unix时间戳转时间(time.Time),支持10位和13位的时间戳,用法: timeT = timeToStr(timeStampToTime("1641139200"), "compact") ,得到20220103000000
"formatTime": tk.FormatTime, // 等同于timeToStr
"strToTime": strToTime, // 字符串转时间
"toTime": tk.ToTime, // 字符串或时间转时间
"bytesToData": tk.BytesToData, // 字节数组转任意类型变量,可选参数-endian=B或L指定使用BigEndian字节顺序还是LittleEndian,函数定义func(bytesA []byte, dataA interface{}, optsA ...string) error,其中dataA为接收变量
"dataToBytes": tk.DataToBytes, // 任意类型值转字节数组,可选参数-endian=B或L指定使用BigEndian字节顺序还是LittleEndian
"toStr": tk.ToStr, // 任意值转字符串
"toInt": tk.ToInt, // 任意值转整数
"toFloat": tk.ToFloat, // 任意值转浮点数
"toByte": tk.ToByte, // 任意值转字节
"toSimpleMap": tk.SimpleMapToString, // 将一个map(map[string]string或map[string]interface{})转换为Simple Map字符串
"fromSimpleMap": tk.LoadSimpleMapFromString, // 将一个Simple Map字符串转换为map[string]string
"hexToBytes": tk.HexToBytes, // 将16进制字符串转换为字节数组([]byte)
"bytesToHex": tk.BytesToHex, // 将字节数组([]byte)转换为16进制字符串
"hexEncode": tk.StrToHex, // 16进制编码
"hex": tk.StrToHex, // 等同于hexEncode
"strToHex": tk.StrToHex, // 等同于hexEncode
"toHex": tk.ToHex, // 将任意值转换为16进制形式,注意是小写格式
"hexDecode": tk.HexToStr, // 16进制解码
"hexToStr": tk.HexToStr, // 等同于hexDecode
"mssToMsi": tk.MSS2MSI, // 转换map[string]string到map[string]interface{}
"msiToMss": tk.MSI2MSS, // 转换map[string]interface{}到map[string]string
"mssToCharMap": charlang.MssToMap, // 转换map[string]string到charlang中的map
"msiToCharMap": charlang.MsiToMap, // 转换map[string]interface{}到charlang中的map
"toInterface": tk.ToInterface, // 任意值转interface{}
"toPointer": tk.ToPointer, // 任意值转相应的指针
"toVar": tk.ToVar, // 任意值(*interface{})转相应的值
// array/map related 数组(切片)/映射(字典)相关
"removeItems": tk.RemoveItemsInArray, // 从切片中删除指定的项,例: removeItems(aryT, 3, 5),注意这是表示删除序号为3到5的项目(序号从0开始),共三项
"removeItem": tk.RemoveItemsInArray, // 等同于removeItems
"remove": tk.RemoveItemsInArray, // 等同于removeItems
"getMapString": tk.SafelyGetStringForKeyWithDefault, // 从映射中获得指定的键值,避免返回nil,函数定义:func getMapString(mapA map[string]string, keyA string, defaultA ...string) string, 不指定defaultA将返回空字符串
"getMapItem": getMapItem, // 类似于getMapString,但可以取任意类型的值
"getArrayItem": getArrayItem, // 类似于getMapItem,但是是取一个切片中指定序号的值
"joinList": tk.JoinList, // 类似于strJoin,但可以连接任意类型的值
// object related 对象有关
"newObject": tk.NewObject, // 新建一个对象,目前支持stack, set(hashset), treeset, list(arraylist), linklist(linkedlist), tree(btree), stringBuffer(stringBuilder), bytesBuffer, error(err), errorString(errStr), string(TXString), StringRing等,用法:objT = newObject("stack")或objT = newObject("tree", 5)创建五层的btree树等
"newObj": tk.NewObject, // 等同于newObject
// error related 错误处理相关
"isError": tk.IsError, // 判断表达式的值是否为error类型
"isErr": tk.IsError, // 等同于isError
"isErrX": tk.IsErrX, // 判断表达式的值是否为error类型,同时也判断是否是TXERROR:开始的字符串
"isErrStr": tk.IsErrStr, // 判断字符串是否是TXERROR:开始的字符串
"checkError": tk.CheckError, // 检查变量,如果是error则立即停止脚本的执行
"checkErr": tk.CheckError, // 等同于checkError
"checkErrf": tk.CheckErrf, // 检查变量,如果是error则立即停止脚本的执行,之前可以printfln输出信息
"checkErrorString": tk.CheckErrorString, // 检查变量,如果是TXERROR:开始的字符串则立即停止脚本的执行
"checkErrStr": tk.CheckErrStr, // 等同于checkErrorString
"checkErrStrf": tk.CheckErrStrf, // 检查变量,如果是TXERROR:开始的字符串则立即停止脚本的执行,之前可以printfln输出信息
"fatalf": tk.Fatalf, // printfln输出信息后终止脚本的执行
"fatalfc": tk.FatalfByCondition, // printfln输出信息后如果第一个参数为false,才终止脚本的执行
"fatalfi": tk.FatalfByCondition, // 同fatalfc
"errStr": tk.ErrStr, // 生成TXERROR:开始的字符串
"errStrf": tk.ErrStrF, // 生成TXERROR:开始的字符串,类似sprintf的用法
"getErrStr": tk.GetErrStr, // 从TXERROR:开始的字符串获取其后的错误信息
"getErrStrX": tk.GetErrStrX, // 从error对象或TXERROR:开始的字符串获取其中的错误信息,返回为空字符串一般表示没有错误
"errf": tk.Errf, // 生成error类型的变量,其中提示信息类似sprintf的用法
"errToEmptyStr": tk.ErrorToEmptyString, // 将任意值转为string,如果是error类型的变量则转为空字符串
// encode/decode related 编码/解码相关
"xmlEncode": tk.EncodeToXMLString, // 编码为XML
"xmlDecode": tk.FromXMLWithDefault, // 解码XML为对象,函数定义:(xmlA string, defaultA interface{}) interface{}
"fromXML": tk.FromXMLX, // 解码XML为etree.Element对象,函数定义:fromXML(xmlA string, pathA ...interface{}) interface{},出错时返回error,否则返回*etree.Element对象
"fromXml": tk.FromXMLX, // 等同于fromXML
"toXML": tk.ToXML, // 编码数据为XML格式,可选参数-indent, -cdata, -root=ABC, -rootAttr={"f1", "v1"}, -default="<xml>ab c</xml>"
"toXml": tk.ToXML, // 等同于toXML
"htmlEncode": tk.EncodeHTML, // HTML编码( 等)
"htmlDecode": tk.DecodeHTML, // HTML解码
"urlEncode": tk.UrlEncode2, // URL编码(http://www.aaa.com -> http%3A%2F%2Fwww.aaa.com)
"urlEncodeX": tk.UrlEncode, // 增强URL编码(会将+和\n等也编码)
"urlDecode": tk.UrlDecode, // URL解码
"base64Encode": tk.EncodeToBase64, // Base64编码,输入参数是[]byte字节数组
"toBase64": tk.ToBase64, // Base64编码,输入参数是[]byte字节数组或字符串
"base64Decode": tk.DecodeFromBase64, // base64解码
"fromBase64": tk.DecodeFromBase64, // 同base64Decode,base64解码
"md5Encode": tk.MD5Encrypt, // MD5编码
"md5": tk.MD5Encrypt, // 等同于md5Encode
"jsonEncode": tk.ObjectToJSON, // JSON编码
"jsonDecode": tk.JSONToObject, // JSON解码
"toJSON": tk.ToJSONX, // 增强的JSON编码,建议使用,函数定义: toJSON(objA interface{}, optsA ...string) string,参数optsA可选。例:s = toJSON(textA, "-indent", "-sort")
"toJson": tk.ToJSONX, // 等同于toJSON
"toJSONX": tk.ToJSONX, // 等同于toJSON
"toJsonX": tk.ToJSONX, // 等同于toJSON
"fromJSON": tk.FromJSONWithDefault, // 增强的JSON解码,函数定义: fromJSON(jsonA string, defaultA ...interface{}) interface{}
"fromJson": tk.FromJSONWithDefault, // 等同于fromJSON
"fromJSONX": fromJSONX, // 增强的JSON解码,建议使用,函数定义: fromJSON(jsonA string) interface{},如果解码失败,返回error对象
"fromJsonX": fromJSONX, // 等同于fromJSONX
"getJSONNode": tk.GetJSONNode, // 获取JSON中的某个节点,未取到则返回nil,示例: getJSONNode("{\"ID\":1,\"Name\":\"Reds\",\"Colors\":[\"Crimson\",\"Red\",\"Ruby\",\"Maroon\"]}", "Colors", 0)
"getJsonNode": tk.GetJSONNode, // 等同于getJSONNode
"simpleEncode": tk.EncodeStringCustomEx, // 简单编码,主要为了文件名和网址名不含非法字符
"simpleDecode": tk.DecodeStringCustom, // 简单编码的解码,主要为了文件名和网址名不含非法字符
"tableToMSSArray": tk.TableToMSSArray, // 参见dbRecsToMapArray,主要用于处理数据库查询结果
"tableToMSSMap": tk.TableToMSSMap, // 类似tableToMSSArray,但还加上一个ID作为主键成为字典/映射类型
"tableToMSSMapArray": tk.TableToMSSMapArray, // 类似tableToMSSMap,但主键下的键值是一个数组,其中每一项是一个map[string]string
// encrypt/decrypt related 加密/解密相关
"encryptStr": tk.EncryptStringByTXDEF, // 加密字符串,第二个参数(可选)是密钥字串
"encryptText": tk.EncryptStringByTXDEF, // 等同于encryptStr
"encryptTextX": fnASSVRSe(tk.EncryptStringByTXDEF), // 等同于encryptStr,但错误时返回error对象
"decryptStr": tk.DecryptStringByTXDEF, // 解密字符串,第二个参数(可选)是密钥字串
"decryptText": tk.DecryptStringByTXDEF, // 等同于decryptStr
"decryptTextX": fnASSVRSe(tk.DecryptStringByTXDEF), // 等同于decryptStr,但错误时返回error对象
"encryptData": tk.EncryptDataByTXDEF, // 加密二进制数据([]byte类型),第二个参数(可选)是密钥字串
"decryptData": tk.DecryptDataByTXDEF, // 解密二进制数据([]byte类型),第二个参数(可选)是密钥字串
// log related 日志相关
"setLogFile": tk.SetLogFile, // 设置日志文件路径,下面有关日志的函数将用到
"logf": tk.LogWithTimeCompact, // 输出到日志文件,函数定义: func logf(formatA string, argsA ...interface{})
"logPrint": logPrint, // 同时输出到标准输出和日志文件
// system related 系统相关
"getClipText": tk.GetClipText, // 从系统剪贴板获取文本,例: textT = getClipText()
"setClipText": tk.SetClipText, // 设定系统剪贴板中的文本,例: setClipText("测试")
"getEnv": tk.GetEnv, // 获取系统环境变量
"setEnv": tk.SetEnv, // 设定系统环境变量
"systemCmd": tk.SystemCmd, // 执行一条系统命令,例如: systemCmd("cmd", "/k", "copy a.txt b.txt")
"openFile": tk.RunWinFileWithSystemDefault, // 用系统默认的方式打开一个文件,例如: openFile("a.jpg")
"ifFileExists": tk.IfFileExists, // 判断文件是否存在
"fileExists": tk.IfFileExists, // 等同于ifFileExists
"joinPath": filepath.Join, // 连接文件路径,等同于Go语言标准库中的path/filepath.Join
"getFileSize": tk.GetFileSizeCompact, // 获取文件大小
"getFileInfo": tk.GetFileInfo, // 获取文件信息,返回map[string]string
"getFileList": tk.GetFileList, // 获取指定目录下的符合条件的所有文件,例:listT = getFileList(pathT, "-recursive", "-pattern=*", "-exclusive=*.txt", "-withDir", "-verbose"), -compact 参数将只给出Abs、Size、IsDir三项
"getFiles": tk.GetFileList, // 等同于getFileList
"createFile": tk.CreateFile, // 等同于tk.CreateFile
"createTempFile": tk.CreateTempFile, // 等同于tk.CreateTempFile
"copyFile": tk.CopyFile, // 等同于tk.CopyFile,可带参数-force和-bufferSize=100000
"removeFile": tk.RemoveFile, // 等同于tk.RemoveFile
"renameFile": tk.RenameFile, // 等同于tk.RenameFile
"loadText": tk.LoadStringFromFile, // 从文件中读取文本字符串,函数定义:func loadText(fileNameA string) string,出错时返回TXERROR:开头的字符串指明原因
"loadTextX": fnASRSE(tk.LoadStringFromFileE), // 从文件中读取文本字符串,函数定义:func loadText(fileNameA string) string,出错时返回error对象
"saveText": tk.SaveStringToFile, // 将字符串保存到文件,函数定义: func saveText(strA string, fileA string) string
"saveTextX": fnASSRSe(tk.SaveStringToFile), // 将字符串保存到文件,如果失败返回error对象
"appendText": tk.AppendStringToFile, // 将字符串增加到文件末尾,函数定义: func appendText(strA string, fileA string) string
"appendTextX": fnASSRSe(tk.AppendStringToFile), // 将字符串增加到文件末尾,如果失败返回error对象
"loadBytes": tk.LoadBytesFromFile, // 从文件中读取二进制数据,函数定义:func loadBytes(fileNameA string, numA ...int) interface{},返回[]byte或error,第二个参数没有或者小于零的话表示读取所有
"loadBytesX": tk.LoadBytesFromFile, // 等同于loadBytes
"saveBytes": tk.SaveBytesToFileE, // 将二进制数据保存到文件,函数定义: func saveBytes(bytesA []byte, fileA string) error
"saveBytesX": tk.SaveBytesToFileE, // 等同于saveBytes
"sleep": tk.Sleep, // 休眠指定的秒数,例:sleep(30),可以是小数
"sleepSeconds": tk.SleepSeconds, // 基本等同于sleep,但只能是整数秒
"sleepMilliSeconds": tk.SleepMilliSeconds, // 类似于sleep,但单位是毫秒
"sleepMS": tk.SleepMilliSeconds, // 等同于sleepMilliSeconds
// time related 时间相关
"now": time.Now, // 获取当前时间
// command-line 命令行处理相关
"getParameter": tk.GetParameterByIndexWithDefaultValue, // 按顺序序号获取命令行参数,其中0代表第一个参数,也就是软件名称或者命令名称,1开始才是第一个参数,注意参数不包括开关,即类似-verbose=true这样的,函数定义:func getParameter(argsA []string, idxA int, defaultA string) string
"getParam": tk.GetParam, // 类似于getParameter,只是后两个参数都是可选,默认是1和""(空字符串),且顺序随意
"getSwitch": tk.GetSwitchWithDefaultValue, // 获取命令行参数中的开关,用法:tmps = getSwitch(args, "-verbose=", "false"),第三个参数是默认值(如果在命令行中没取到的话返回该值)
"getIntSwitch": tk.GetSwitchWithDefaultIntValue, // 与getSwitch类似,但获取到的是整型(int)的值
"getFloatSwitch": tk.GetSwitchWithDefaultFloatValue, // 与getSwitch类似,但获取到的是浮点数(float64)的值
"switchExists": tk.IfSwitchExistsWhole, // 判断命令行参数中是否存在开关(完整的,),用法:flag = switchExists(args, "-restart")
"ifSwitchExists": tk.IfSwitchExistsWhole, // 等同于switchExists
"parseCommand": tk.ParseCommandLine, // 等同于tk.ParseCommandLine
// network related 网络相关
"newSSHClient": tk.NewSSHClient, // 新建一个SSH连接,以便执行各种SSH操作,例:
// clientT, errT = newSSHClient(hostName, port, userName, password)
// defer clientT.Close() // 别忘了用完关闭网络连接
// outT, errT = clientT.Run(`ls -p; cat abc.txt`) // 执行一条或多条命令
// errT = clientT.Upload(`./abc.txt`, strReplace(joinPath(pathT, `abc.txt`), `\`, "/")) // 上传文件
// errT = clientT.Download(`down.txt`, `./down.txt`) // 下载文件
// bytesT, errT = clientT.GetFileContent(`/root/test/down.txt`) // 获取某个文件的二进制内容[]byte
"mapToPostData": tk.MapToPostData, // 从一个映射(map)对象生成进行POST请求的参数对象,函数定义func mapToPostData(postDataA map[string]string) url.Values
"getWebPage": tk.DownloadPageUTF8, // 进行一个网络HTTP请求并获得服务器返回结果,或者下载一个网页,函数定义func getWebPage(urlA string, postDataA url.Values, customHeaders string, timeoutSecsA time.Duration, optsA ...string) string
// customHeadersA 是自定义请求头,内容是多行文本形如 charset: utf-8。如果冒号后还有冒号,要替换成`
// 返回结果是TXERROR字符串,即如果是以TXERROR:开头,则表示错误信息,否则是网页或请求响应
"getWeb": tk.DownloadWebPageX, // 进行一个网络HTTP请求并获得服务器返回结果,或者下载一个网页,函数定义func getWebPage(urlA string, postDataA map[string]string, optsA ...string) string
// 除了urlA,所有参数都是可选;
// optsA支持-verbose, -detail, -timeout=30(秒),-encoding=utf-8/gb2312/gbk/gb18030等,
// 如果要添加FORM形式的POST的数据,则直接传入一个url.Values类型的数据,或者map[string]string或者map[string]interface{}的参数即可,也可以用开关参数-post={"Key1": "Value1", "Key2": "Value2"}这样传入JSON,此时请求将自动转为POST方式(默认是GET方式)
// 如果要直接POST数据,则直接传入-postBody=ABCDEFG这样的信息即可,其中ABCDEFG是所需POST的字符串,例如getWeb("http://abc.com:8001/sap/bc/srt/rfc/sap/getSvc", "-postBody=<XML><data1>Test</data1></XML>", `-headers={"Content-Type":"text/xml; charset=utf-8", "SOAPAction":""}`, "-timeout=15"),此时请求将自动转为POST方式(默认是GET方式),另外也可以直接传入一个[]byte类型的参数
// 如需添加自定义请求头,则添加开关参数类似:-headers={"content-type": "text/plain; charset=utf-8;"}
// 返回结果是TXERROR字符串,即如果是以TXERROR:开头,则表示错误信息,否则是网页或请求响应
"downloadFile": tk.DownloadFile, // 从网络下载一个文件,函数定义func downloadFile(urlA, dirA, fileNameA string, argsA ...string) string
"httpRequest": tk.RequestX, // 进行一个网络HTTP请求并获得服务器返回结果,函数定义func httpRequest(urlA, methodA, reqBodyA string, customHeadersA string, timeoutSecsA time.Duration, optsA ...string) (string, error)
// 其中methodA可以是"GET","POST"等
// customHeadersA 是自定义请求头,内容是多行文本形如 charset: utf-8。如果冒号后还有冒号,要替换成`
"postRequest": tk.PostRequestX, // 进行一个POST网络请求并获得服务器返回结果,函数定义func postRequest(urlA, reqBodyA string, customHeadersA string, timeoutSecsA time.Duration, optsA ...string) (string, error)
// 其中reqBodyA是POST的body
// customHeadersA 是自定义请求头,内容是多行文本形如 charset: utf-8。如果冒号后还有冒号,要替换成`
// timeoutSecsA是请求超时的秒数
// optsA是一组字符串,可以是-verbose和-detail,均表示是否输出某些信息
"getFormValue": tk.GetFormValueWithDefaultValue, // 从HTTP请求中获取字段参数,可以是Query参数,也可以是POST参数,函数定义func getFormValue(reqA *http.Request, keyA string, defaultA string) string
"formValueExist": tk.IfFormValueExists, // 判断HTTP请求中的是否有某个字段参数,函数定义func formValueExist(reqA *http.Request, keyA string) bool
"ifFormValueExist": tk.IfFormValueExists, // 等同于formValueExist
"formToMap": tk.FormToMap, // 将HTTP请求中的form内容转换为map(字典/映射类型),例:mapT = formToMap(req.Form)
"generateJSONResponse": tk.GenerateJSONPResponseWithMore, // 生成Web API服务器的JSON响应,支持JSONP,例:return generateJSONResponse("fail", sprintf("数据库操作失败:%v", errT), req)
"genResp": tk.GenerateJSONPResponseWithMore, // 等同于generateJSONResponse
"writeResp": tk.WriteResponse, // 写http输出,函数原型writeResp(resA http.ResponseWriter, strA string) error
"writeRespHeader": tk.WriteResponseHeader, // 写http响应头的状态(200、404等),函数原型writeRespHeader(resA http.ResponseWriter, argsA ...interface{}) error,例:writeRespHeader(http.StatusOK)
"setRespHeader": tk.SetResponseHeader, // 设置http响应头中的内容,函数原型setRespHeader(resA http.ResponseWriter, keyA string, valueA string) error,例:setRespHeader(responseG, "Content-Type", "text/json; charset=utf-8")
"jsonRespToHtml": tk.JSONResponseToHTML, // 类似{"Status":"fail", "Value":"failed to connect DB"}的JSON响应转换为通用的简单的错误网页
"replaceHtmlByMap": tk.ReplaceHtmlByMap,
"cleanHtmlPlaceholders": tk.CleanHtmlPlaceholders,
// database related
"dbConnect": sqltk.ConnectDBX, // 连接数据库以便后续读写操作,例:
// dbT = dbConnect("sqlserver", "server=127.0.0.1;port=1443;portNumber=1443;user id=user;password=userpass;database=db1")
// if isError(dbT) {
// fatalf("打开数据库%v错误:%v", dbT)
// }
// }
// defer dbT.Close()
"dbClose": sqltk.CloseDBX, // 关闭数据库连接,例:
// errT := dbClose(dbT)
// if isError(rs) {
// fatalf("关闭数据库时发生错误:%v", rs)
// }
// }
"dbExec": sqltk.ExecDBX, // 进行数据库操作,例:
// rs := dbExec(dbT, `insert into table1 (field1,id,field2) values('value1',1,'value2')`
// if isError(rs) {
// fatalf("新增数据库记录时发生错误:%v", rs)
// }
// }
// insertID, affectedRows = rs[0], rs[1]
"dbQuery": sqltk.QueryDBX, // 进行数据库查询,所有字段结果都将转换为字符串,返回结果为[]map[string]string,用JSON格式表达类似:[{"Field1": "Value1", "Field2": "Value2"},{"Field1": "Value1a", "Field2": "Value2a"}],例:
// sqlRsT = dbQuery(dbT, `SELECT * FROM TABLE1 WHERE ID=3`)
// if isError(sqlRsT) {
// fatalf("查询数据库错误:%v", dbT)
// }
// pl("在数据库中找到%v条记录", len(sqlRsT))
"dbQueryRecs": sqltk.QueryDBRecsX, // 进行数据库查询,所有字段结果都将转换为字符串,返回结果为[][]string,即二维数组,其中第一行为表头字段名:[["Field1", "Field2"],["Value1","Value2"]],例:
// sqlRsT = dbQueryRecs(dbT, `SELECT * FROM TABLE1 WHERE ID=3`)
// if isErr(sqlRsT) {
// fatalf("查询数据库错误:%v", sqlRsT)
// }
// pl("在数据库中找到%v条记录", len(sqlRsT))
"dbQueryMap": sqltk.QueryDBMapX, // 进行数据库查询,所有字段结果都将转换为字符串,返回结果为map[string]map[string]string,即将dbQuery的结果再加上一个索引,例:{"Value1": {"Field1": "Value1"}, "Value2": {"Field2": "Value2"}}
// sqlRsT = dbQueryMap(dbT, `SELECT * FROM TABLE1 WHERE ID=3`, "ID")
// if isErr(sqlRsT) {
// fatalf("查询数据库错误:%v", sqlRsT)
// }
// pl("在数据库中找到结果:%v", sqlRsT)
"dbQueryMapArray": sqltk.QueryDBMapArrayX, // 进行数据库查询,所有字段结果都将转换为字符串,返回结果为map[string][]map[string]string,即将dbQueryMap的结果中,每一个键值中可以是一个数组([]map[string]string类型),例:{"Value1": [{"Field1": "Value1"}, {"Field1": "Value1a"}], "Value2": [{"Field1": "Value2"}, {"Field1": "Value2a"}, {"Field1": "Value2b"}]}
// sqlRsT = dbQueryMapArray(dbT, `SELECT * FROM TABLE1 WHERE ID=3`, "ID")
// if isErr(sqlRsT) {
// fatalf("查询数据库错误:%v", sqlRsT)
// }
// pl("在数据库中找到结果:%v", sqlRsT)
"dbQueryCount": sqltk.QueryCountX, // 与dbQuery类似,但主要进行数量查询,也支持结果只有一个整数的查询,例:
// sqlRsT = dbQueryCount(dbT, `SELECT COUNT(*) FROM TABLE1 WHERE ID>3`)
// if isError(sqlRsT) {
// fatalf("查询数据库错误:%v", dbT)
// }
// pl("在数据库中共有符合条件的%v条记录", sqlRsT)
"dbQueryFloat": sqltk.QueryFloatX, // 与dbQueryCount类似,但主要进行返回一个浮点数结果的查询,例:
// sqlRsT = dbQueryFloat(dbT, `SELECT PRICE FROM TABLE1 WHERE ID=3`)
// if isError(sqlRsT) {
// fatalf("查询数据库错误:%v", dbT)
// }
// pl("查询结果为%v", sqlRsT)
"dbQueryString": sqltk.QueryStringX, // 与dbQueryCount类似,但主要支持结果只有一个字符串的查询
"dbFormat": sqltk.FormatSQLValue, // 将字符串转换为可用在SQL语句中的字符串(将单引号变成双单引号)
"formatSQLValue": sqltk.FormatSQLValue, // 将字符串转换为可用在SQL语句中的字符串(将单引号变成双单引号)
"dbOneLineRecordToMap": sqltk.OneLineRecordToMap, // 将只有一行(加标题行两行)的SQL语句查询结果([][]string格式)变为类似{"Field1": "Value1", "Field2": "Value2"}的map[string]string格式
"dbOneColumnRecordsToArray": sqltk.OneColumnRecordsToArray, // 将只有一列的SQL语句查询结果([][]string格式)变为类似["Value1", "Value2"]的[]string格式
"dbRecsToMapArray": sqltk.RecordsToMapArray, // 将多行行(第一行为标头字段行)的SQL语句查询结果([][]string格式)变为类似[{"Field1": "Value1", "Field2": "Value2"},{"Field1": "Value1a", "Field2": "Value2a"}]的[]map[string]string格式
"dbRecsToMapArrayMap": sqltk.RecordsToMapArrayMap, // 将多行行(第一行为标头字段行)的SQL语句查询结果([][]string格式)变为类似dbQueryMapArray函数返回的结果
// line editor related 内置行文本编辑器有关
"leClear": leClear, // 清空行文本编辑器缓冲区,例:leClear()
"leLoadStr": leLoadString, // 行文本编辑器缓冲区载入指定字符串内容,例:leLoadStr("abc\nbbb\n结束")
"leSetAll": leLoadString, // 等同于leLoadString
"leSaveStr": leSaveString, // 取出行文本编辑器缓冲区中内容,例:s = leSaveStr()
"leGetAll": leSaveString, // 等同于leSaveStr
"leLoad": leLoadFile, // 从文件中载入文本到行文本编辑器缓冲区中,例:err = leLoad(`c:\test.txt`)
"leLoadFile": leLoadFile, // 等同于leLoad
"leSave": leSaveFile, // 将行文本编辑器缓冲区中内容保存到文件中,例:err = leSave(`c:\test.txt`)
"leSaveFile": leSaveFile, // 等同于leSave
"leLoadClip": leLoadClip, // 从剪贴板中载入文本到行文本编辑器缓冲区中,例:err = leLoadClip()
"leSaveClip": leSaveClip, // 将行文本编辑器缓冲区中内容保存到剪贴板中,例:err = leSaveClip()
"leLoadUrl": leLoadUrl, // 从网址URL载入文本到行文本编辑器缓冲区中,例:err = leLoadUrl(`http://example.com/abc.txt`)
"leInsert": leInsertLine, // 行文本编辑器缓冲区中的指定位置前插入指定内容,例:err = leInsert(3, "abc")
"leInsertLine": leInsertLine, // 等同于leInsert
"leAppend": leAppendLine, // 行文本编辑器缓冲区中的最后追加指定内容,例:err = leAppendLine("abc")
"leAppendLine": leAppendLine, // 等同于leAppend
"leSet": leSetLine, // 设定行文本编辑器缓冲区中的指定行为指定内容,例:err = leSet(3, "abc")
"leSetLine": leSetLine, // 等同于leSet
"leSetLines": leSetLines, // 设定行文本编辑器缓冲区中指定范围的多行为指定内容,例:err = leSetLines(3, 5, "abc\nbbb")
"leRemove": leRemoveLine, // 删除行文本编辑器缓冲区中的指定行,例:err = leRemove(3)
"leRemoveLine": leRemoveLine, // 等同于leRemove
"leRemoveLines": leRemoveLines, // 删除行文本编辑器缓冲区中指定范围的多行,例:err = leRemoveLines(1, 3)
"leViewAll": leViewAll, // 查看行文本编辑器缓冲区中的所有内容,例:allText = leViewAll()
"leView": leViewLine, // 查看行文本编辑器缓冲区中的指定行,例:lineText = leView(18)
"leSort": leSort, // 将行文本编辑器缓冲区中的行进行排序,唯一参数表示是否降序排序,例:errT = leSort(true)
"leEnc": leConvertToUTF8, // 将行文本编辑器缓冲区中的文本转换为UTF-8编码,如果不指定原始编码则默认为GB18030编码
"leLineEnd": leLineEnd, // 读取或设置行文本编辑器缓冲区中行末字符(一般是\n或\r\n),不带参数是获取,带参数是设置
"leSilent": leSilent, // 读取或设置行文本编辑器的静默模式(布尔值),不带参数是获取,带参数是设置
// GUI related start
// gui related 图形界面相关
"initGUI": initGUI, // GUI操作,一般均需调用initGUI来进行初始化,例:initGUI()
"getConfirmGUI": getConfirmGUI, // 显示一个提示信息并让用户确认的对话框,例:getConfirmGUI("对话框标题", "信息内容"),注意,从第二个参数开始可以类似于printf那样带格式化字符串和任意长度参数值,例如getConfirmGUI("对话框标题", "信息内容=%v", abc)
"getInputGUI": getInputGUI, // 显示一个提示信息并让用户输入信息的对话框,例:getInputGUI("请输入……", "姓名"),注意,从第3个参数开始为可选参数,可以有-ok=确认按钮标题,-cancel=取消按钮标题,分别表示确认按钮与取消按钮的标题(默认分别为OK和Cancel),例如getInputGUI("对话框标题", "信息内容", "-ok=确定", "-cancel=关闭"),返回输入字符串,如果按了取消按钮,将返回TXERROR:开始的空字符串
"getPasswordGUI": getPasswordGUI, // 显示一个提示信息并让用户输入密码/口令的对话框,例:getPasswordGUI("请输入……", "密码"),注意,从第3个参数开始为可选参数,可以有-ok=确认按钮标题,-cancel=取消按钮标题,分别表示确认按钮与取消按钮的标题(默认分别为OK和Cancel),例如getPasswordGUI("对话框标题", "信息内容", "-ok=确定", "-cancel=关闭")
"getListItemGUI": getListItemGUI, // 提供单选列表供用户选择,结果格式是选中的字符串或者TXERROR字符串;示例:getListItemGUI("请选择", "所需的颜色", ["红色","黄色"]...)
"getListItemsGUI": getListItemsGUI, // 提供多选列表供用户选择,结果格式是选中的字符串数组或者TXERROR字符串;示例:getListItemGUI("请选择", "所需的颜色", ["红色","黄色","蓝色"]...)
"getColorGUI": getColorGUI, // 获取用户选择的颜色,结果格式是FFEEDD或者TXERROR字符串;示例:getColorGUI("请选择颜色", "CCCCCC")
"getDateGUI": getDateGUI, // 获取用户选择的日期,结果格式是20210218或者TXERROR字符串;示例:getDateGUI("请选择……", "开始日期"),注意,从第二个参数开始可以类似于printf那样带格式化字符串和任意长度参数值,例如getPasswordGUI("对话框标题", "信息内容=%v", abc)
"showInfoGUI": showInfoGUI, // 显示一个提示信息的对话框,例:showInfoGUI("对话框标题", "信息内容"),注意,从第二个参数开始可以类似于printf那样带格式化字符串和任意长度参数值,例如showInfoGUI("对话框标题", "信息内容=%v", abc)
"showErrorGUI": showErrorGUI, // 显示一个错误或警告信息的对话框,例:showErrorGUI("对话框标题", "错误或警告内容"),注意,从第二个参数开始可以类似于printf那样带格式化字符串和任意长度参数值,例如showErrorGUI("对话框标题", "信息内容=%v", abc)
"selectFileToSaveGUI": selectFileToSaveGUI, // 图形化选取用于保存数据的文件,例:fileName = selectFileToSaveGUI("-title=请选择文件……", "-filterName=所有文件", "-filter=*", "-start=."),参数均为可选,start是默认起始目录
"selectFileGUI": selectFileGUI, // 图形化选取文件,例:fileName = selectFileGUI("-title=请选择文件……", "-filterName=所有文件", "-filter=*", "-start=."),参数均为可选,start是默认起始目录
"selectDirectoryGUI": selectDirectoryGUI, // 图形化选取目录,例:dirName = selectDirectoryGUI("-title=请选择目录……", "-start=."),参数均为可选,start是默认起始目录
// GUI related end
// misc 杂项函数
"sortX": tk.SortX, // 排序各种数据,用法:sort([{"f1": 1}, {"f1": 2}], "-key=f1", "-desc")
"newFunc": NewFuncB, // 将Gox语言中的定义的函数转换为Go语言中类似 func f() 的形式
"newFuncIIE": NewFuncInterfaceInterfaceErrorB, // 将Gox语言中的定义的函数转换为Go语言中类似 func f(a interface{}) (interface{}, error) 的形式
"newFuncSSE": NewFuncStringStringErrorB, // 将Gox语言中的定义的函数转换为Go语言中类似 func f(a string) (string, error) 的形式
"newFuncSS": NewFuncStringStringB, // 将Gox语言中的定义的函数转换为Go语言中类似 func f(a string) string 的形式
"newCharFunc": newCharFunc, // 将Gox语言中的定义的函数转换为Charlang语言中类似 func f() 的形式
"newStringRing": tk.NewStringRing, // 创建一个字符串环,大小固定,后进的会将先进的最后一个顶出来
"getCfgStr": getCfgString, // 从根目录(Windows下为C:\,*nix下为/)的gox子目录中获取文件名为参数1的配置项字符串
"setCfgStr": setCfgString, // 向根目录(Windows下为C:\,*nix下为/)的gox子目录中写入文件名为参数1,内容为参数2的配置项字符串,例:saveCfgStr("timeout", "30")
"genQR": tk.GenerateQR, // 生成二维码,例:genQR("http://www.example.com", "-level=2"), level 0..3,越高容错性越好,但越大
"newChar": charlang.NewChar, // new a charlang script VM
"runChar": charlang.RunChar, // run a charlang script VM
"runCharCode": charlang.RunCharCode, // run a charlang script
"runXie": xie.RunCode, // run a xielang script
"quickCompileChar": charlang.QuickCompile, // compile a charlang script VM
"quickRunChar": charlang.QuickRun, // run a charlang script VM
"newCharAny": charlang.NewAny, // create a interface{} pointer in charlang
"newCharAnyValue": charlang.NewAnyValue, // create a interface{} value in charlang
"toCharValue": charlang.ConvertToObject, // convert to a interface{} value in charlang
"wrapError": tk.WrapError, //
"renderMarkdown": tk.RenderMarkdown, // 将Markdown格式字符串渲染为HTML
"genToken": tk.GenerateToken, // 生成令牌,用法:genToken("appCode", "userID", "userRole", "-secret=abc")
"checkToken": tk.CheckToken, // 检查令牌,如果成功,返回类似“appCode|userID|userRole|”的字符串;失败返回TXERROR字符串
// global variables 全局变量
"timeFormatG": tk.TimeFormat, // 用于时间处理时的时间格式,值为"2006-01-02 15:04:05"
"timeFormatCompactG": tk.TimeFormatCompact, // 用于时间处理时的简化时间格式,值为"20060102150405"
"getSystemEndian": tk.GetSystemEndian, // 获取系统的字节顺序,返回binary.BigEndian或binary.LittleEndian
"getStack": getStack, // 获取堆栈
"getVars": getVars, // 获取当前变量表
"scriptPathG": scriptPathG, // 所执行脚本的路径
"versionG": versionG, // Gox/Goxc的版本号
"leBufG": leBufG, // 内置行文本编辑器所用的编辑缓冲区
以及
imisc包,用于生成纯Go语言的函数变量
"NewFunc": NewFunc,
"NewFuncError": NewFuncError,
"NewFuncInterface": NewFuncInterface,
"NewFuncInterfaceError": NewFuncInterfaceError,
"NewFuncInterfaceInterfaceError": NewFuncInterfaceInterfaceError,
"NewFuncInterfaceInterfaceErrorB": NewFuncInterfaceInterfaceErrorB,
"NewFuncIntString": NewFuncIntString,
"NewFuncIntError": NewFuncIntError,
"NewFuncFloatString": NewFuncFloatString,
"NewFuncFloatStringError": NewFuncFloatStringError,
"NewFuncStringString": NewFuncStringString,
"NewFuncStringError": NewFuncStringError,
"NewFuncStringStringError": NewFuncStringStringError,
"NewFuncStringStringErrorB": NewFuncStringStringErrorB,
"NewFuncIntStringError": NewFuncIntStringError,
例如,如果需要一个纯Go语言的函数(有时候一些包中需要传递函数参数),类似 func myprint(a string) (string, error),那么可以这样
func myprint(a) {
pl("%v", a)
return "", errf("")
}
myprintv = imisc.NewFuncStringStringError(^myprint)
w.Bind("myprint", *myprintv)
其中,w.Bind函数就需要一个func (a string) (string, error)类型的函数作为参数传入。
输出相关
- printfln或pl
相当于printf函数并且会在最后多输出一个回车换行符。
- println或pln
相当于其他语言中的println函数。
相当于其他语言中的print函数。
- printf
相当于其他语言中的printf函数。
- pv
pv函数可以查看变量的名称、类型和值,在调试代码的时候比较方便,但注意函数的参数要求传入变量名称,是个字符串,要加双引号。
- plv
plv(v)相当于printfln("%#v", v)。
- plerr
plerr函数用于输出一个error类型的值的信息。
- type或typeOf
用于获得一个变量或数值的类型。
- eval
计算一个表达式的值。
- exit
退出整个程序的运行。
- checkError
检查一个error变量是否是非nil值,如果不是nil,则输出error的信息并退出程序。
- checkErrorString
检查一个字符串是否是表示错误的字符串(以TXERROR:开头),如果是,则输出提示信息并退出程序。
- setValue
设置一个指针变量指向的值。
- getValue
获取一个指针变量指向的值。
- bitXor
按位异或(bitwise XOR)。
- remove
从数组中删除从起始索引到结束索引的所有数值项,例如 a = remove(a, 1, 3)。
- setVar
设置一个门户全局变量,用于与其他脚本共享数据。
- getVar
获取一个门户全局变量的值,用于获取其他脚本共享的数据。
- getInput
等待并获取用户命令行输入。
- getInputf
等待并获取用户命令行输入,并在之前用类似printf的方式给出提示信息。
- run
执行一个文件中的脚本。
- edit
调用内置代码编辑器编辑脚本。
- newSSHClient
新建一个SSH连接,以便执行各种SSH操作。
- runScript
运行一个脚本。
- getClipText
获取剪贴板文本。
- setClipText
设置剪贴板文本。
最新的内置函数说明请参考下面的英文文档或这里
Variables
argsG
get global variable
The global value "argsG" could be used for retrieve command-line arguments, and the first element(the Gox executable) is usually removed. If you need the whole command-line, use os.Args instead.
scriptPathG
holds the full script path(include the file name), for network scenes, will be ""(empty string)
Functions
Note: some functions may exist or not in different script engine, and may have some slight differences.
pass
A function doing nothing. Usually used at the end of a script to ensure return nothing(so that will print nothing by the VM).
defined
check if a variable is defined
eval
evaluate an expression and return the result
typeof/typeOf/kindOf
return the string representation of the type for a variable or expression
a = 1
println(typeof(a))
remove
remove one or several items from an array
remove(arrayA, startIndexA, endIndexA)
print/pr
the same as fmt.Print
println/pln
the same as fmt.Println
printf/prf
the same as fmt.Printf
printfln/pl
the same as fmt.Printf but add a new-line character at the end
sprintf
the same as fmt.Sprintf
fprintln/fprintf
the same as fmt.Fprintln/fmt.Fprintf
plv
the same as pl("%#v", v)
pv
output the name, type, value of a variable, attention: the parameter passed to this function should be a string, and only the global varibles are allowed.
s2 = "abcabcabc"
pv("s2")
// the output ->
// s2(string): abcabcabc
plvsr
the same as pl("%#v", v), but for various arguments
plerr
a convenient way to print an error value
exit
the same as os.Exit(1), used to terminate\exit the whole script running
setValue
assign a value by a pointer, used in Qlang engine.
s = new(string)
// *s = "abc" is not correct in Qlang engine
setValue(s, "abc")
println(*s) // use * for dereference a value from pointer is allowed
getValue
get a value referenced by a pointer, used in Qlang engine.
s = new(string)
// *s = "abc" is not correct in Qlang engine
setValue(s, "abc")
println(*s) // use * for dereference a value from pointer is allowed
v = getValue(s)
println(v) // will be "abc"
setVar
set global variable
getVar
get global variable
bitXor
bitwise XOR operation, since in Qlang engine, ^ is used for get address/pointer of a variable(like & in other engine),
so the origin bitwise XOR operator in Golang is used and we will use bitXor function instead.
checkError
a convenient way to print an error value if not nil, and terminate the whole program running
outT, errT = clientT.Run(cmdT)
checkError(errT, deferFunc)
deferFunc is the function which will be called before terminating the application, if none, pass nil for it, i.e. checkError(errT, nil)
checkErrorString
the same as checkError, but check a TXERROR string
isErrStr
check if is TXERROR string (which starts with TXERROR:)
errStr
generate a TXERROR string (which starts with TXERROR:)
errStrf
generate a TXERROR string with format like printf, the same as tk.ErrStrF
getErrStr
remove the prefix(TXERROR:) of a TXERROR string, return the result
errf
the same as tk.Errf, generate an error
getInput(deprecated)
get user input from command-line
printf("A:")
a = getInput()
printf("B:")
b = getInput()
println("A + B =", a+b)
getInputf
the same as getInput, but use printf to print a prompt string
n = 3
a = getInputf("Please enter the %v value: ", n)
printf("B:")
b = getInput()
println("A + B =", a+b)
panic
raise a panic manually
try {
panic("a manual panic")
} catch e {
printfln("error: %v", e)
} finally {
println("final")
}
try {
panic(12345678)
} catch e {
printfln(e)
}
keys
get the keys of a map
range
range an array, actually a keyword, not a function
a = [1, 2, "list"]
for i, v = range a {
println(a)
}
deepClone
deep copy a struct variable and generate a new one
usage:
person1 = make(struct {
Name string,
Age int
})
person1.Name = "John"
person1.Age = 20
pl("%#v", person1)
person2 = person1
person2.Name ="Tom"
pv("person1")
pv("person2")
p3 = deepClone(&person1)
p3 = *p3
p3.Name = "abc"
pv("person1")
pv("person2")
pv("p3")
deepCopy
deep copy a struct variable to another one
getClipText
get clipboard text
setClipText
set clipboard text
trim
trim a string
run
execute a script file(in a new created VM)
run(argsA...)
runCode
execute a script string(in a new created VM, argsG will hold the arguments)
runCode(codeA string, argsA ...string)
runScript
runScript(scriptA, modeA, argsA ...)
modeA == "" || modeA == "1" || modeA == "new"
run script in a new VM
any other modeA will run it as a system command.
magic
execute a script with a magic number(in a new created VM, argsG will hold the arguments)
magic(numberA, argsA...)
systemCmd
run a system command
systemCmd(cmdA, argsA ...)
newSSHClient
create a SSH client to run shell commands, upload/download file from a remote server. Thanks to melbahja and visit here to find more docs.
short examples:
clientT, errT = newSSHClient(hostName, port, userName, password)
outT, errT = clientT.Run(`ls -p; cat abc.txt`)
errT = clientT.Upload(`./abc.txt`, tk.Replace(tk.JoinPath(pathT, `abc.txt`), `\`, "/"))
errT = clientT.Download(`down.txt`, `./down.txt`)
find more in this example script here
getParameter
the same as tk.GetParameterByIndexWithDefaultValue
getSwitch
the same as tk.GetSwitchWithDefaultValue
switchExists
the same as tk.IfSwitchExistsWhole