当前位置 : 主页 > 编程语言 > java >

【大家的项目】Gostd update to v0.2.6 ---- 新增了httpclient实现

来源:互联网 收集:自由互联 发布时间:2022-06-23
go std 在 Rust 中的实现 todo Go基础类型在rust实现,在gostd::builtin 中,比如 int64 = i64, int32 = i32 强制转换宏,例如 2 as i64 等价 int64!(2) 跟Go的int64(2)就多个! time库在rust实现 gostd::time time库支持

go std 在 Rust 中的实现

todo

  • Go基础类型在rust实现,在gostd::builtin 中,比如 int64 = i64, int32 = i32
  • 强制转换宏,例如 2 as i64 等价 int64!(2) 跟Go的int64(2)就多个!
  • time库在rust实现 gostd::time
  • time库支持macOSX 和linux平台,通过libc库调用C函数实现 time::Now()
  • time,支持各种格式显示时间。
  • docs.rs文档增加例子程序"RUN"按钮,但是要复制代码本地运行,在rust play运行不了(因为下载量没到前100)。
  • time支持local时区信息自动从系统读取,可以用time::Now()获取本地时间。
  • strings模块,字符串处理函数已完成。除了Reader相关方法还没全实现。(version >=0.2.3)
  • strings模块, 完成剩余的方法实现。(version >=0.2.4)
  • io模块,完成部分接口例如Reader,Writer等。(version >=0.2.4)
  • http模块的client客户端功能,支持http和https。(version >=0.2.6)

独立发布包

独立发布gostd_time,代码等价于 use gostd::time 。

独立发布gostd_builtin, 代码等价于 use gostd::builtin 。

使用例子

http模块

client客户端

  • POST
  • 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(())
    }

    或者

    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"}

  • GET
  • 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(())
    }

    或者

    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模块

  • 字符串替换 strings::ReplaceAll()
  • use gostd::strings;

    fn main() {

    assert_eq!(
    "moo moo moo",
    strings::ReplaceAll("oink oink oink", "oink", "moo")
    );
    }

  • 字符串分割 strings::Split()
  • 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"));
    }

  • 字符串位置查找 strings::Index
  • use gostd::strings;

    fn main() {

    assert_eq!(4, strings::Index("chicken", "ken"));
    assert_eq!(-1, strings::Index("chicken", "dmr"));
    }

  • 将多个字符串连接成一个新字符串 strings::Join
  • use gostd::strings;

    fn main() {

    let s = vec!["foo", "bar", "baz"];
    assert_eq!("foo, bar, baz", strings::Join(s, ", "));
    }

  • 字符串转换成大写 strings::ToUpper
  • use gostd::strings;

    fn main() {

    assert_eq!("GOPHER", strings::ToUpper("Gopher"));
    }

  • 字符串转换成小写 strings::ToLower
  • use gostd::strings;

    fn main() {

    assert_eq!("gopher", strings::ToLower("Gopher"));
    }

    time模块

    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);
    }


    上一篇:Rust社区运营3周年总结
    下一篇:没有了
    网友评论