阅读本文时,可观看计算机编程语言的本质重置版_哔哩哔哩_bilibili
或是阅读计算机编程语言的本质-腾讯云开发者社区-腾讯云一文对于理解本文的意思也会有帮助。
后端这么多的编程语言背后有没有规律
这么多的编程语言及其构建的各种框架生态,各种轮子,各种库,它们背后就没有一个统一的东西吗?当然有,就像我们学的数学一样,数学题目有N个,但是它们背后都有一个公式法则,掌握了公式法则就能解开各种题目。
可以阅读或观看前面的2个链接试图理解背后的规律。
这么多的编程语言它们的运行环境都有什么共同的东西呢
1 运行在统一的OS系统
没有OS,咱们写的程序怎么运行?怎么装载?【这里不考虑嵌入式裸机编程或是单片机】
2 运行在统一的指令集架构
在互联网做程序员开发,咱们的程序运行环境大部分都是这样的。
在互联网公司几乎使用Linux os 作为服务器,程序员编写的程序最终运行在Linux os 上。
这么多的编程语言它们都有一个共同的需求
就是TCP/IP协议。
TCP/IP协议的支持由Linux os 内核提供相应的socket api接口【系统调用接口Syscall】
java 的应用层TCP/IP具体应用
JAVA语言的spring框架接收GET和POST参数
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
@SpringBootApplication
@RestController
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@GetMapping("/hello")
public String hello(@RequestParam(name = "name", required = false) String name) {
if (name != null) {
return "Hello, " + name + "!";
} else {
return "Hello, World!";
}
}
@PostMapping("/user")
public String createUser(@RequestParam("username") String username, @RequestParam("email") String email) {
// 在此处根据参数创建用户
return "User created: " + username + ", " + email;
}
}
php 的应用层TCP/IP具体应用
php语言的laravel框架接收GET和POST参数
<?php
use Illuminate\Http\Request;
Route::get('/hello', function (Request $request) {
$name = $request->input('name', 'World');
return "Hello, $name!";
});
Route::post('/user', function (Request $request) {
$username = $request->input('username');
$email = $request->input('email');
// 在此处根据参数创建用户
return "User created: $username, $email";
});
go的应用层TCP/IP具体应用
go语言的gin框架接收GET和POST参数
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
router := gin.Default()
router.GET("/hello", func(c *gin.Context) {
name := c.Query("name")
if name == "" {
name = "World"
}
c.String(http.StatusOK, "Hello, %s!", name)
})
router.POST("/user", func(c *gin.Context) {
username := c.PostForm("username")
email := c.PostForm("email")
// 在此处根据参数创建用户
c.String(http.StatusOK, "大家好啊,我是北风,我微信Le-studyg欢迎加我搞网恋啊: %s, %s", username, email)
})
router.Run(":8080")
}
python的应用层TCP/IP具体应用
python语言的django框架接收GET和POST参数
from django.http import HttpResponse
from django.views.decorators.http import require_http_methods
@require_http_methods(["GET"])
def hello(request):
name = request.GET.get('name', 'World')
return HttpResponse(f"Hello 北风, {name}!")
@require_http_methods(["POST"])
def create_user(request):
username = request.POST.get('username')
email = request.POST.get('email')
# 在此处根据参数创建用户
return HttpResponse(f"User created: {username}, {email}")
node的应用层TCP/IP具体应用
node语言的express框架接收GET和POST参数
const express = require('express');
const app = express();
const port = 3000;
app.get('/hello', (req, res) => {
const name = req.query.name || 'World';
res.send(`Hello, ${name}!`);
});
app.post('/user', (req, res) => {
const username = req.body.username;
const email = req.body.email;
// 在此处根据参数创建用户
res.send(`User created: ${username}, ${email}`);
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
rust的应用层TCP/IP具体应用
rust语言的actix-web框架接收GET和POST参数
use actix_web::{web, App, HttpResponse, HttpServer};
async fn hello(name: web::Query<String>) -> HttpResponse {
let name = name.into_inner();
HttpResponse::Ok().body(format!("Hello, {}!", name))
}
async fn create_user(info: web::Form<UserInfo>) -> HttpResponse {
let username = info.username.clone();
let email = info.email.clone();
// 在此处根据参数创建用户
HttpResponse::Ok().body(format!("User created: {}, {}", username, email))
}
#[derive(serde::Deserialize)]
struct UserInfo {
username: String,
email: String,
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(web::resource("/hello").route(web::get().to(hello)))
.service(web::resource("/user").route(web::post().to(create_user)))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
上面演示了这么多语言各框架的生态,它们的共同点都是要:数据传输即数据接收,数据发送
本质上就是TCP/IP的使用,而TCP/IP的接口即系统调用syscall接口由Linux os 提供【因为互联网大部分项目都是运行在Linux os 上居多】。
完成数据传输的系统调用接口【看到这里不明白的话可以先看前面的2个链接】即
socket() bind() listen() accept() epoll,select ,write,sendmsg,recvmsg....
前面这么多的编程语言它们在Linux os 运行实现数据传输时都用到了这些系统调用接口【TCP/IP的使用】
而这些socket api 【socket ,bind ,listen ,accept ....】就是TCP/IP协议的应用层具体使用,它们跟 编程语言没有什么关系,因为这些系统调用它是内核提供的指令集合,已经很多年没有变了。掌握这些api的使用本质上就是我们所说的网络编程。
网络编程技术是你驾驭各种编程语言构建千变万化的应用一个核心技术
数据传输的API:write ,sendmsg,recvmsg,sendto,recv,recvfrom ....
1 前面我们演示了java ,php,golang,python,node,rust这些语言的各框架生态它们都用到了数据传输
2 这些编程语言构建出来的应用不计其数如mycat,netty,swoole,tornado,django,spring,express,mysql,redis,nginx,tomcat..............
它们都有一个共同的东西:数据传输【TCP/IP,网络编程】
网络编程技术的学习到底有什么好处
这里随便截图网络上的一些招聘,它们的要求都跟网络编程技术的掌握有关系
当然还有很多这里不一一列出了。
网络编程技术跟编程语言有什么关系
所有的编程语言都要用Linux os socket api的接口,掌握了这套技术并积攒了经验就能轻松的使用。
跟语言没有什么太大的关系。
那么如果我是PHP的话怎么系统的学习呢:并发编程到网络通信引擎架构设计_PHP学习路线-51CTO学堂
如果我是GO的话怎么系统的学习呢:Golang核心高级【共109课时】_Go语言课程-51CTO学堂
如果我是其它语言的使用者怎么学习呢:Linux C核心技术【共153课时】_C/C++课程-51CTO学堂或是Linux X86-64汇编/GDB/项目实战