前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >技术阅读周刊第十期

技术阅读周刊第十期

作者头像
crossoverJie
发布2023-12-19 16:29:05
1440
发布2023-12-19 16:29:05
举报
文章被收录于专栏:crossoverJiecrossoverJie

Golang: 14 Shorthand Tricks You Might Not Know! | by Nidhi D | Dec, 2023 | Canopas

URL: https://blog.canopas.com/golang-14-shorthand-tricks-you-might-not-know-8d8d21954c49

同时声明和初始化变量

代码语言:javascript
复制
// Long form  
var message string  
message = "Hello, Golang!"  
  
// Shorthand  
message := "Hello, Golang!"

声明和初始化多个变量

代码语言:javascript
复制
// Long form  
var a, b, c int  
a = 1  
b = 2  
c = 3  
  
// Shorthand  
a, b, c := 1, 2, 3

交换变量

代码语言:javascript
复制
a, b := 1, 2  
  
// Long form  
temp := a  
a = b  
b = temp  
  
// Shorthand  
a, b = b, a

Defer 函数调用

代码语言:javascript
复制
// Long form  
func cleanup() {  
// Cleanup logic  
}  
defer cleanup()  
  
// Shorthand  
defer func() {  
// Cleanup logic  
}()

检测 Map 中的数据是否存在

代码语言:javascript
复制
// Long form  
value, exists := myMap[key]  
if !exists {  
    // Key doesn't exist in the map  
}  
  
// Shorthand  
if value, exists := myMap[key]; !exists {  
    // Key doesn't exist in the map  
}

使用下标和值迭代切片

代码语言:javascript
复制
// Long form  
for i := 0; i < len(numbers); i++ {  
fmt.Println(i, numbers[i])  
}  
  
// Shorthand  
for i, value := range numbers {  
fmt.Println(i, value)  
}

错误检测

代码语言:javascript
复制
// Long form  
result, err := someFunction()  
if err != nil {  
// Handle the error  
}  
  
// Shorthand  
if result, err := someFunction(); err != nil {  
// Handle the error  
}

创建一个变量的指针

代码语言:javascript
复制
// Long form  
var x int  
ptr := &x  
  
// Shorthand  
ptr := new(int)

匿名函数

代码语言:javascript
复制
// Long form  
func add(x, y int) int {  
return x + y  
}  
  
// Shorthand  
add := func(x, y int) int {  
return x + y  
}

创建和初始化 Map

代码语言:javascript
复制
// Long form  
colors := make(map[string]string)  
colors["red"] = "#ff0000"  
colors["green"] = "#00ff00"  
  
// Shorthand  
colors := map[string]string{  
"red": "#ff0000",  
"green": "#00ff00",  
}

声明多个常量

代码语言:javascript
复制
// Long form  
const pi float64 = 3.14159  
const maxAttempts int = 3  
  
// Shorthand  
const (  
 pi = 3.14159  
 maxAttempts = 3  
)

Java Mastery Unleashed: 12 Essential Tips Every Developer Must Embrace

URL: https://blog.stackademic.com/boost-your-java-skills-12-must-know-programming-tips-for-java-developers-34f8381ec431

一些常用的 Java 技巧

  • 善用 Lambda 表达式
代码语言:javascript
复制
// Before
List<String> names = new ArrayList<>();  
for (Person person : people) {  
names.add(person.getName());  
}
// After
List<String> names = people.stream()  
.map(Person::getName)  
.collect(Collectors.toList());
  • 使用 Optionals 替代 null
代码语言:javascript
复制
Optional<String> maybeName = Optional.ofNullable(person.getName());  
String name = maybeName.orElse("Unknown");
  • 使用 stream 简化集合操作
代码语言:javascript
复制
List<Integer> evenNumbers = numbers.stream()  
.filter(num -> num % 2 == 0)  
.collect(Collectors.toList());
  • String.format 拼接字符串
代码语言:javascript
复制
String s1 = "Hello";  
String s2 = " World";  
String s = String.format("%s%s", s1, s2);
  • 使用 default method 扩展接口
代码语言:javascript
复制
import java.time.LocalDateTime;  
public interface TimeClient {  
void setTime(int hour, int minute, int second);  
void setDate(int day, int month, int year);  
void setDateAndTime(int day, int month, int year, int hour, int minute, int second);  
LocalDateTime getLocalDateTime();  
}
  • 使用枚举替换常量
代码语言:javascript
复制
public class Main {
    enum Level { LOW, MEDIUM, HIGH }
    public static void main(String[] args) {
        Level myVar = Level.MEDIUM;
        System.out.println(myVar);
    }
}
  • 使用 try-with-Resource 管理资源
代码语言:javascript
复制
try (FileReader fileReader = new FileReader("example.txt");
     BufferedReader bufferedReader = new BufferedReader(fileReader)) {
    String line = bufferedReader.readLine();
    // Process the file
} catch (IOException e) {
    // Handle the exception
}

SpringBoot Webflux vs Vert.x: Performance comparison for hello world case | Tech Tonic

URL: https://medium.com/deno-the-complete-reference/springboot-webflux-vs-vert-x-performance-comparison-for-hello-world-case-41a6bd8e9f8c

本文对比了 SpringBoot Webflux 和 Vert.x 的性能对比

以下是两个框架写的压测接口:

代码语言:javascript
复制
package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.reactivestreams.Publisher;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Mono;

@SpringBootApplication
@EnableWebFlux
@Controller
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }

    @GetMapping("/")
    @ResponseBody
    public Publisher<String> handler() {
        return Mono.just("Hello world!");
    }
}

// Vert.x
package com.example.starter;  
  
import io.vertx.core.AbstractVerticle;  
import io.vertx.core.Promise;  
import io.vertx.core.http.HttpServer;  
import io.vertx.ext.web.Router;  
  
public class MainVerticle extends AbstractVerticle {  
  
  @Override  
  public void start(Promise<Void> startPromise) throws Exception {  
    HttpServer server = vertx.createHttpServer();  
    Router router = Router.router(vertx);  
  
    router.get("/").respond(ctx -> ctx  
        .response()  
        .putHeader("Content-Type", "text/plain")  
        .end("hello world!"));  
  
    server.requestHandler(router).listen(3000);  
  }  
}

最后直接看对比结果吧:

最终作者根据一个计算公式得出两个框架的得分,规则如下:

  • 差距小于 5% 不得分
  • 5~20 得 1 分
  • 20~50 得两分
  • 大于 50,得三分

最终是 Vert.x 得分超过 Webflux 55%??

不过个人觉得压测结果再好,套上业务后,比如一个接口查询了多个后端服务,后端服务有依赖于多个数据库,最终出来的 RT 大家都差不多。

除非是某些对性能极致要求的场景,比如实时数据分析、物联网中间件等和直接业务不太相关领域。

它的底层依然是 Netty,但比 Netty 提供了跟易用的 API。

Git Cherry Pick Examples to Apply Hot Fixes and Security Patches — Nick Janetakis

URL: https://nickjanetakis.com/blog/git-cherry-pick-examples-to-apply-hot-fixes-and-security-patches?ref=dailydev

讲解了 git cherry-pick 的作用,什么时候该用,什么时候不用。

举个例子:一些大型的开源项目往往都会有一个主分支,同时维护了不同版本的子分支,有些用户可能就会一直使用一些长期维护的子分支,比如 v2.1.0 \ v2.3.0

但对于大部分的开发者来说主要会维护主分支,也会在主分支上推进一些新功能,这些新功能不一定会同步到上述提到的两个老版本中。

但对于一些安全漏洞,重大 bug 等是需要同步到这些子分支的,但又不能把一些不兼容的新特性同步到子分支中。

此时就可以使用 cherry-pick 这个功能,只将某一个提交给 pick 到目标分支中。

代码语言:javascript
复制
# Cherry pick more than 1 SHA.
#
# This could be useful if you have a handful of commits that you want to bring over,
# you'll likely want to order them with the oldest commit being first in the list.
git cherry-pick <SHA> <SHA>

# Edit the git commit message for the newly applied commit.
#
# This could be useful if want to customize the git commit message with extra context.
git cherry-pick <SHA> --edit

# Avoid automatically creating the commit which lets you edit the files first.
#
# This could be useful if you need to make manual code adjustments before committing,
# such as applying a security patch which uses an older library with a different API.
git cherry-pick <SHA> --no-commit
本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-12-16,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 crossoverJie 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Golang: 14 Shorthand Tricks You Might Not Know! | by Nidhi D | Dec, 2023 | Canopas
    • 同时声明和初始化变量
      • 声明和初始化多个变量
        • 交换变量
          • Defer 函数调用
            • 检测 Map 中的数据是否存在
              • 使用下标和值迭代切片
                • 错误检测
                  • 创建一个变量的指针
                    • 匿名函数
                      • 创建和初始化 Map
                        • 声明多个常量
                        • Java Mastery Unleashed: 12 Essential Tips Every Developer Must Embrace
                        • SpringBoot Webflux vs Vert.x: Performance comparison for hello world case | Tech Tonic
                        • Git Cherry Pick Examples to Apply Hot Fixes and Security Patches — Nick Janetakis
                        相关产品与服务
                        消息队列 TDMQ
                        消息队列 TDMQ (Tencent Distributed Message Queue)是腾讯基于 Apache Pulsar 自研的一个云原生消息中间件系列,其中包含兼容Pulsar、RabbitMQ、RocketMQ 等协议的消息队列子产品,得益于其底层计算与存储分离的架构,TDMQ 具备良好的弹性伸缩以及故障恢复能力。
                        领券
                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
                        http://www.vxiaotou.com