Rust vs Go:常用语法对比(6)(1)

简介: Rust vs Go:常用语法对比(6)(1)

101. Load from HTTP GET request into a string

Make an HTTP request with method GET to URL u, then store the body of the response in string s.

发起http请求

package main
import (
  "fmt"
  "io/ioutil"
  "net"
  "net/http"
)
func main() {
  u := "http://" + localhost + "/hello?name=Inigo+Montoya"
  res, err := http.Get(u)
  check(err)
  buffer, err := ioutil.ReadAll(res.Body)
  res.Body.Close()
  check(err)
  s := string(buffer)
  fmt.Println("GET  response:", res.StatusCode, s)
}
const localhost = "127.0.0.1:3000"
func init() {
  http.HandleFunc("/hello", myHandler)
  startServer()
}
func myHandler(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintf(w, "Hello %s", r.FormValue("name"))
}
func startServer() {
  listener, err := net.Listen("tcp", localhost)
  check(err)
  go http.Serve(listener, nil)
}
func check(err error) {
  if err != nil {
    panic(err)
  }
}

res has type *http.Response. buffer has type []byte. It is idiomatic and strongly recommended to check errors at each step.

extern crate reqwest;
use reqwest::Client;
let client = Client::new();
let s = client.get(u).send().and_then(|res| res.text())?;

or

[dependencies]
ureq = "1.0"
let s = ureq::get(u).call().into_string()?;

or

[dependencies]
error-chain = "0.12.4"
reqwest = { version = "0.11.2", features = ["blocking"] }
use error_chain::error_chain;
use std::io::Read;
let mut response = reqwest::blocking::get(u)?;
let mut s = String::new();
response.read_to_string(&mut s)?;

102. Load from HTTP GET request into a file

Make an HTTP request with method GET to URL u, then store the body of the response in file result.txt. Try to save the data as it arrives if possible, without having all its content in memory at once.

发起http请求

package main
import (
  "fmt"
  "io"
  "io/ioutil"
  "net"
  "net/http"
  "os"
)
func main() {
  err := saveGetResponse()
  check(err)
  err = readFile()
  check(err)
  fmt.Println("Done.")
}
func saveGetResponse() error {
  u := "http://" + localhost + "/hello?name=Inigo+Montoya"
  fmt.Println("Making GET request")
  resp, err := http.Get(u)
  if err != nil {
    return err
  }
  defer resp.Body.Close()
  if resp.StatusCode != 200 {
    return fmt.Errorf("Status: %v", resp.Status)
  }
  fmt.Println("Saving data to file")
  out, err := os.Create("/tmp/result.txt")
  if err != nil {
    return err
  }
  defer out.Close()
  _, err = io.Copy(out, resp.Body)
  if err != nil {
    return err
  }
  return nil
}
func readFile() error {
  fmt.Println("Reading file")
  buffer, err := ioutil.ReadFile("/tmp/result.txt")
  if err != nil {
    return err
  }
  fmt.Printf("Saved data is %q\n", string(buffer))
  return nil
}
const localhost = "127.0.0.1:3000"
func init() {
  http.HandleFunc("/hello", myHandler)
  startServer()
}
func myHandler(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintf(w, "Hello %s", r.FormValue("name"))
}
func startServer() {
  listener, err := net.Listen("tcp", localhost)
  check(err)
  go http.Serve(listener, nil)
}
func check(err error) {
  if err != nil {
    panic(err)
  }
}

resp has type http.Response. It is idiomatic and strongly recommended to check errors at each step, except for the calls to Close.

Making GET request
Saving data to file
Reading file
Saved data is "Hello Inigo Montoya"
Done.
extern crate reqwest;
use reqwest::Client;
use std::fs::File;
let client = Client::new();
match client.get(&u).send() {
    Ok(res) => {
        let file = File::create("result.txt")?;
        ::std::io::copy(res, file)?;
    },
    Err(e) => eprintln!("failed to send request: {}", e),
};

105. Current executable name

Assign to string s the name of the currently executing program (but not its full path).

当前可执行文件名称

将当前正在执行的程序的名称分配给字符串s(但不是它的完整路径)。

package main
import (
  "fmt"
  "os"
  "path/filepath"
)
func main() {
  var s string
  path := os.Args[0]
  s = filepath.Base(path)
  fmt.Println(s)
}
fn get_exec_name() -> Option<String> {
    std::env::current_exe()
        .ok()
        .and_then(|pb| pb.file_name().map(|s| s.to_os_string()))
        .and_then(|s| s.into_string().ok())
}
fn main() -> () {
    let s = get_exec_name().unwrap();
    println!("{}", s);
}

or

fn main() {
    let s = std::env::current_exe()
        .expect("Can't get the exec path")
        .file_name()
        .expect("Can't get the exec name")
        .to_string_lossy()
        .into_owned();
    println!("{}", s);
}

106. Get program working directory

Assign to string dir the path of the working directory. (This is not necessarily the folder containing the executable itself)

获取程序的工作路径

package main
import (
  "fmt"
  "os"
)
func main() {
  dir, err := os.Getwd()
  fmt.Println(dir, err)
}
use std::env;
fn main() {
    let dir = env::current_dir().unwrap();
    println!("{:?}", dir);
}

107. Get folder containing current program

Assign to string dir the path of the folder containing the currently running executable. (This is not necessarily the working directory, though.)

获取包含当前程序的文件夹

package main
import (
  "fmt"
  "os"
  "path/filepath"
)
func main() {
  var dir string
  programPath := os.Args[0]
  absolutePath, err := filepath.Abs(programPath)
  if err != nil {
    panic(err)
  }
  dir = filepath.Dir(absolutePath)
  fmt.Println(dir)
}
let dir = std::env::current_exe()?
    .canonicalize()
    .expect("the current exe should exist")
    .parent()
    .expect("the current exe should be a file")
    .to_string_lossy()
    .to_owned();

Rust doesn't represent paths as Strings, so we need to convert the Path returned from Path::parent. This code chooses to do this lossily, replacing characters it doesn't recognize with �


109. Number of bytes of a type

Set n to the number of bytes of a variable t (of type T).

获取某个类型的字节数

package main
import (
  "fmt"
  "reflect"
)
func main() {
  var t T
  tType := reflect.TypeOf(t)
  n := tType.Size()
  fmt.Println("A", tType, "object is", n, "bytes.")
}
type Person struct {
  FirstName string
  Age       int
}
// T is a type alias, to stick to the idiom statement.
// T has the same memory footprint per value as Person.
type T Person
// T has (8 + 4) == 12 bytes of data
struct T(f64, i32);
fn main() {
    let n = ::std::mem::size_of::<T>();
    println!("{} bytes", n);
    // T has size 16, which is "the offset in bytes between successive elements in an array with item type T"
}

110. Check if string is blank

Set boolean blank to true if string s is empty, or null, or contains only whitespace ; false otherwise.

检查字符串是否空白

package main
import (
  "fmt"
  "strings"
)
func main() {
  for _, s := range []string{
    "",
    "a",
    " ",
    "\t \n",
    "_",
  } {
    blank := strings.TrimSpace(s) == ""
    if blank {
      fmt.Printf("%q is blank\n", s)
    } else {
      fmt.Printf("%q is not blank\n", s)
    }
  }
}
"" is blank
"a" is not blank
" " is blank
"\t \n" is blank
"_" is not blank
fn main() {
    let list = vec!["", " ", "  ", "\t", "\n", "a", " b "];
    for s in list {
        let blank = s.trim().is_empty();
        if blank {
            println!("{:?}\tis blank", s)
        } else {
            println!("{:?}\tis not blank", s)
        }
    }
}
""  is blank
" " is blank
"  "  is blank
"\t"  is blank
"\n"  is blank
"a" is not blank
" b " is not blank
目录
相关文章
|
1月前
|
Rust API
【Rust学习】09_方法语法
结构体让你可以创建出在你的领域中有意义的自定义类型。通过结构体,我们可以将相关联的数据片段联系起来并命名它们,这样可以使得代码更加清晰。在 impl 块中,你可以定义与你的类型相关联的函数,而方法是一种相关联的函数,允许您指定结构体的实例具有的行为。 但是结构体并不是创建自定义类型的唯一方式:让我们转向 Rust 的 enum 功能,将另一个工具添加到你的工具箱中。
17 0
|
2月前
|
Java 编译器 Go
Go to Learn Go之基础语法
Go to Learn Go之基础语法
19 0
|
3月前
|
Rust 安全 编译器
30天拿下Rust之语法大全
Rust是一种系统级编程语言,以其独特的所有权系统和内存安全性受到开发者青睐。本文从基本数据类型入手,介绍了标量类型如整数、浮点数、布尔值及字符,复合类型如元组、数组和结构体等。此外,还探讨了变量与常量的声明与使用,条件判断与循环语句的语法,以及函数定义与调用的方法。文章通过示例代码展示了如何使用Rust编写简洁高效的程序,并简要介绍了注释与宏的概念,为读者快速掌握这门语言提供了实用指南。欲获取最新文章或交流技术问题,请关注微信公众号“希望睿智”。
53 1
|
2月前
|
Rust Linux Go
Rust/Go语言学习
Rust/Go语言学习
|
5月前
|
存储 Java Go
|
4月前
|
Rust
Rust 中使用 :: 这种语法的几种情况
Rust 中使用 :: 这种语法的几种情况
|
5月前
|
Rust
Rust的if let语法:更简洁的模式匹配
Rust的if let语法:更简洁的模式匹配
|
5月前
|
编译器 Go 开发者
|
5月前
|
Go
go基础语法结束篇 ——函数与方法
go基础语法结束篇 ——函数与方法
|
5月前
|
编译器 Go 数据安全/隐私保护
go语言入门之路——基础语法
go语言入门之路——基础语法
下一篇
无影云桌面