Crates.io | gostd |
lib.rs | gostd |
version | 0.3.19 |
source | src |
created_at | 2021-09-16 06:46:53.571636 |
updated_at | 2024-07-31 06:31:41.695299 |
description | gostd is the go standard library implementation in rust. gostd 是Go标准库的rust实现 |
homepage | https://github.com/wandercn/gostd |
repository | https://github.com/wandercn/gostd |
max_upload_size | |
id | 452127 |
size | 312,308 |
Gostd is the golang standard library implementation in rust-lang.
Gostd是rust-lang中的golang标准库实现。
rust 语法比go复杂,但是go代码简单好理解,想通过这个项目把go的标准库通过rust实现。以后有什么go的项目可以通过它方便翻译代码到rust,比如把 import "bytes" 改成 use gostd::bytes 就可以实现转换。
本项目纯粹个人兴趣,大概率会失败,但是梦想还是要有的万一它实现了。
go to rust,to be rust or to be failed.
go 这个包不会实现,因为我们转换成rust基本用不上这个包。
├── archive
├── bufio
├── builtin
├── bytes
├── cmd
├── compress
├── container
├── context
├── crypto
├── database
├── debug
├── embed
├── encoding
├── errors
├── expvar
├── flag
├── fmt
├── go
├── hash
├── html
├── image
├── index
├── internal
├── io
├── log
├── math
├── mime
├── net
├── os
├── path
├── plugin
├── reflect
├── regexp
├── runtime
├── sort
├── strconv
├── strings
├── sync
├── syscall
├── testdata
├── testing
├── text
├── time
├── unicode
├── unsafe
└── vendor
use gostd::archive
use gostd::bufio
use gostd::builtin
use gostd::bytes
use gostd::cmd
use gostd::compress
use gostd::container
use gostd::context
use gostd::crypto
use gostd::database
use gostd::debug
use gostd::embed
use gostd::encoding
use gostd::errors
use gostd::expvar
use gostd::flag
use gostd::fmt
use gostd::go
use gostd::hash
use gostd::html
use gostd::image
use gostd::index
use gostd::internal
use gostd::io
use gostd::log
use gostd::math
use gostd::mime
use gostd::net
use gostd::os
use gostd::path
use gostd::plugin
use gostd::reflect
use gostd::regexp
use gostd::runtime
use gostd::sort
use gostd::strconv
use gostd::strings
use gostd::sync
use gostd::syscall
use gostd::testdata
use gostd::testing
use gostd::text
use gostd::time
use gostd::unicode
use gostd::unsafe
use gostd::vendor
独立发布gostd_time,代码等价于 use gostd::time 。
独立发布gostd_builtin, 代码等价于 use gostd::builtin 。
use gostd::bytes;
use gostd::mime::multipart::Writer;
use gostd::net::http::{Client, Method, Request};
fn main() -> Result<(), std::io::Error> {
let mut body = bytes::Buffer::new();
let mut w = Writer::new(&mut body);
w.WriteField("requestId", "12121231231")?;
w.WriteField("testTime", "2022-01-22 18:00:00")?;
w.WriteField("checkTime", "2022-01-22 22:00:00")?;
w.WriteField("auditTime", "2022-01-22 23:00:00")?;
w.WriteField("tubeCode", "QCGD99SDF")?;
w.WriteField("testRatio", "1")?;
w.WriteField("name", "刘xxx")?;
w.WriteField("sex", "1")?;
w.WriteField("birthdate", "20003-07-02")?;
w.WriteField("address", "北京市丰台区")?;
w.WriteField("phoneNumber", "1881xxxx")?;
w.WriteField("cardType", "身份证")?;
w.WriteField("cardNumber", "xxxx")?;
w.WriteField("testResult", "0")?;
w.WriteField("testUserName", "xxx")?;
w.WriteField("checkUserName", "xxx")?;
w.Close()?;
let contentType = w.FormDataContentType();
let url = "http://www.baidu.com";
let mut req = Request::New(Method::Post, url, Some(body.Bytes()))?;
req.Header.Set("Content-Type", contentType.as_str());
let mut client = Client::New();
let response = client.Do(&mut req)?;
println!(
"{}",
String::from_utf8(response.Body.expect("return body error")).unwrap()
);
Ok(())
}
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"}
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(())
}
use gostd::strings;
fn main() {
assert_eq!(
"moo moo moo",
strings::ReplaceAll("oink oink oink", "oink", "moo")
);
}
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"));
}
use gostd::strings;
fn main() {
assert_eq!(4, strings::Index("chicken", "ken"));
assert_eq!(-1, strings::Index("chicken", "dmr"));
}
use gostd::strings;
fn main() {
let s = vec!["foo", "bar", "baz"];
assert_eq!("foo, bar, baz", strings::Join(s, ", "));
}
use gostd::strings;
fn main() {
assert_eq!("GOPHER", strings::ToUpper("Gopher"));
}
use gostd::strings;
fn main() {
assert_eq!("gopher", strings::ToLower("Gopher"));
}