Gostd update to v0.2.6 ---- 新增了httpclient实现

wandercn · · 889 次点击 · 开始浏览    置顶
这是一个创建于 的主题,其中的信息可能已经有所发展或是发生改变。

[https://github.com/wandercn/gostd](https://github.com/wandercn/gostd) # todo - [x] Go基础类型在rust实现,在gostd::builtin 中,比如 int64 = i64, int32 = i32 - [x] 强制转换宏,例如 2 as i64 等价 int64!(2) 跟Go的int64(2)就多个! - [x] time库在rust实现 gostd::time - [x] time库支持macOSX 和linux平台,通过libc库调用C函数实现 time::Now() - [x] time,支持各种格式显示时间。 - [x] docs.rs文档增加例子程序"RUN"按钮,但是要复制代码本地运行,在rust play运行不了(因为下载量没到前100)。 - [x] time支持local时区信息自动从系统读取,可以用time::Now()获取本地时间。 - [x] strings模块,字符串处理函数已完成。除了Reader相关方法还没全实现。(version >=0.2.3) - [x] strings模块, 完成剩余的方法实现。(version >=0.2.4) - [x] io模块,完成部分接口例如Reader,Writer等。(version >=0.2.4) - [x] http模块的client客户端功能,支持http和https。(version >=0.2.6) # 独立发布包 独立发布gostd_time,代码等价于 use gostd::time 。 独立发布gostd_builtin, 代码等价于 use gostd::builtin 。 # 使用例子 ## http模块 ### client客户端 1. POST ```go use gostd::net::http; fn main() -> Result<(), std::io::Error> { let url = "https://petstore.swagger.io/v2/pet"; let postbody = r#"{"id":0,"category":{"id":0,"name":"string"},"name":"doggie","photoUrls":["string"],"tags":[{"id":0,"name":"string"}],"status":"available"}"# .as_bytes() .to_vec(); let response = http::Post(url, "application/json", Some(postbody))?; println!( "{}", String::from_utf8(response.Body.expect("return body error")).unwrap() ); Ok(()) } ``` 或者 ```go use gostd::net::http::{Client, Method, Request}; fn main() -> Result<(), std::io::Error> { let url = "https://petstore.swagger.io/v2/pet"; let postbody = r#"{ "id": 0, "category": { "id": 0, "name": "string" }, "name": "doggie", "photoUrls": [ "string" ], "tags": [ { "id": 0, "name": "string" } ], "status": "available" }"# .as_bytes() .to_vec(); let mut req = Request::New(Method::Post, url, Some(postbody))?; req.Header.Set("accept", "application/json"); req.Header.Set("Content-Type", "application/json"); let mut client = Client::New(); let response = client.Do(&mut req)?; println!( "{}", String::from_utf8(response.Body.expect("return body error")).unwrap() ); Ok(()) } // output // {"id":92233723685477587,"category":{"id":,"name":"string"},"name":"doggie","photoUrls":["string"],"tags":[{"id":,"name":"string"}],"status":"available"} ``` 2. GET ```go use gostd::net::http; fn main() -> Result<(), std::io::Error> { let url = "https://petstore.swagger.io/v2/pet/findByStatus?status=available"; let response = http::Get(url)?; println!( "{}", String::from_utf8(response.Body.expect("return body error")).unwrap() ); Ok(()) } ``` 或者 ```go use gostd::net::http::{Client, Method, Request}; fn main() -> Result<(), std::io::Error> { let url = "https://petstore.swagger.io/v2/pet/findByStatus?status=available"; let mut req = Request::New(Method::Get, url, None)?; req.Header.Set("Content-Type", "application/json"); let mut client = Client::New(); let response = client.Do(&mut req)?; println!( "{}", String::from_utf8(response.Body.expect("return body error")).unwrap() ); Ok(()) } ``` ## strings模块 1. 字符串替换 strings::ReplaceAll() ```go use gostd::strings; fn main() { assert_eq!( "moo moo moo", strings::ReplaceAll("oink oink oink", "oink", "moo") ); } ``` 2. 字符串分割 strings::Split() ```go use gostd::strings; fn main() { assert_eq!(vec!["a", "b", "c"], strings::Split("a,b,c", ",")); assert_eq!( vec!["", "man ", "plan ", "canal panama"], strings::Split("a man a plan a canal panama", "a ") ); assert_eq!( vec!["", " ", "x", "y", "z", " ", ""], strings::Split(" xyz ", "") ); assert_eq!(vec![""], strings::Split("", "Bernardo O'Higgins")); } ``` 3. 字符串位置查找 strings::Index ```go use gostd::strings; fn main() { assert_eq!(4, strings::Index("chicken", "ken")); assert_eq!(-1, strings::Index("chicken", "dmr")); } ``` 4. 将多个字符串连接成一个新字符串 strings::Join ```go use gostd::strings; fn main() { let s = vec!["foo", "bar", "baz"]; assert_eq!("foo, bar, baz", strings::Join(s, ", ")); } ``` 5. 字符串转换成大写 strings::ToUpper ```go use gostd::strings; fn main() { assert_eq!("GOPHER", strings::ToUpper("Gopher")); } ``` 6. 字符串转换成小写 strings::ToLower ```go use gostd::strings; fn main() { assert_eq!("gopher", strings::ToLower("Gopher")); } ``` ## time模块 ```go fn main() { use gostd_time as time; let mut t = time::Date(2009, 11, 10, 14, 30, 12, 13, time::UTC.clone()); assert_eq!( t.String(), "2009-11-10 14:30:12.000000013 +0000 UTC".to_string() ); println!("UTC: {}", t); let cst_zone = time::FixedZone("CST", 8 * 3600); //北京时区 CST = UTC时区+8小时 t.In(cst_zone); assert_eq!( t.String(), "2009-11-10 22:30:12.000000013 +0800 CST".to_string() ); println!("CST: {}", t); } ```

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

889 次点击  
加入收藏 微博
2 回复  |  直到 2023-04-18 10:59:56
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传