
编程语言
shida_csdn
Keep Running ...
展开
-
0.1 + 0.2 == 0.3 吗?
1. 背景这要从一段 golang 代码讲起:func main() { var a float32 = 0.1 var b float32 = 0.2 var c float32 = 0.3 fmt.Println(a + b == c) // true var aa float64 = 0.1 var bb float64...原创 2020-03-01 23:53:05 · 1464 阅读 · 0 评论 -
Golang unsafe 指针转换与内存操作
Golang 提供了 unsafe 包,让我们能够直接操作指定内存地址的内存。通过 unsafe.Pointer() 函数,我们能够获取变量的内存地址,本质上这是个整数。但 Pointer 不支持运算,如果要在内存地址上进行加减运算,需要将其转为 uintptr 类型。下面我们尝试读取切片地址,并通过内存操作遍历其内容:package mainimport "fmt&am原创 2019-03-04 11:05:10 · 3349 阅读 · 0 评论 -
Go sync.RWMutex 实现线程安全 map 读写
关键词:读写锁当然,go 语言已经内置提供了线程安全 map,即 sync.Map,在这里只是用自己的方式实现简单的锁应用,代码示例如下:import "sync"type SafeDict struct { data map[string]int *sync.RWMutex}func NewSafeDict(data map[string]int) *SafeD...原创 2019-03-07 11:30:13 · 1946 阅读 · 0 评论 -
Golang 中 Channel 对阻塞 goroutine 的唤醒顺序分析
一、前言 我们知道,goroutine 是有大小的,当 发送满/读取空时,会阻塞对应的 发送/读取 goroutine 协程。 那么,当 Channel 可用时,它是按照什么顺序唤醒等待的 goroutine 协程的呢? 带着这个问题,我们深入 chan 的源码逻辑,去一探究竟。 剧透结论:先阻塞的先被唤醒!二、chan 源码分析...原创 2019-03-08 12:30:21 · 4521 阅读 · 0 评论 -
goroutine 队列插入顺序分析
一、背景 我们在编写 go 程序时,经常会创建一系列的协程,这些协程通常会被放到 P 维护的一个协程队列 那么协程被插入队列时遵循的顺序是如何的呢?到底是插到队头还是队尾呢?二、源码分析 golang 版本: 1.11 源码位置:runtime/proc.go// Create a new g running fn with...原创 2019-03-25 12:16:37 · 986 阅读 · 0 评论 -
[leetcode-in-go] 0001-Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same e...原创 2019-06-08 10:46:33 · 218 阅读 · 0 评论 -
[leetcode-in-go] 0002-Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i...原创 2019-06-08 11:24:47 · 188 阅读 · 0 评论 -
[leetcode-in-go] 0003-Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Example 1:Input: “abcabcbb”Output: 3Explanation: The answer is “abc”, with the length of 3.Example 2:Input: ...原创 2019-06-09 10:36:16 · 268 阅读 · 0 评论 -
[leetcode-in-go] 0004-Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).You may assume nums1 and num...原创 2019-06-10 23:19:03 · 163 阅读 · 0 评论 -
godep 安装使用介绍
一、前言 godep 是解决包依赖的管理工具,也是目前最主流的一种 golang 项目包管理工具 kubernetes 等主流项目都在使用,相比直接 vendor 方式, godep 在其基础上增加了版本控制的概念二、安装 官方项目 github 地址:https://github.com/tools/godep 安装比较...原创 2019-01-15 10:39:55 · 3690 阅读 · 0 评论 -
[Linux] Shell 必知必会
1. 带提示的输入#!/bin/bashread -p "input file name:" FILENAMEecho $FILENAME2. 常用判断标志-c 字符设备-d 文件夹3. 参数相关 $0 脚本名称; $1 第一个参数4. switch-case 语句case $1 in 'start') ech...原创 2018-03-08 23:14:20 · 484 阅读 · 0 评论 -
[Go] CentOS安装 Go 环境
本文介绍如何在 CentOS 操作系统安装 Go 语言编译环境基本步骤:1. 下载安装包 go1.10.linux-amd64.tar.gz 地址:https://studygolang.com/dl2. 解压到指定目录# tar -C /usr/local -xzf go1.10.linux-amd64.tar.gz3. 配置环境变量...原创 2018-03-05 09:29:21 · 13341 阅读 · 3 评论 -
Python 实现区块链 Just 模拟
import hashlibimport randomimport stringimport jsonimport threadingfrom decimal import Decimalfrom time import timeclass MyThread(threading.Thread): def __init__(self, target, args=()):...原创 2018-03-13 21:13:37 · 537 阅读 · 0 评论 -
Thread ThreadLocal ThreadLocalMap 关系
1 . Thread 类里有一个变量,是 ThreadLocalMap 类型的,名为 threadLocals;2. ThreadLocalMap 的 key 为 ThreadLocal 变量引用, value 即为 ThreadLocal 存储的值;3. 一般使用方法是建一个独立的类,里边存放 ThreadLocal 常量,以供其他线程使用class MyThreadLoc...原创 2018-04-24 10:12:34 · 662 阅读 · 0 评论 -
String 源码解读
1. 字符串连接 + 的实现原理String concatenation is implemented through the {@code StringBuilder}(or {@code StringBuffer}) class and its {@code append} method.// 连接操作是通过 sb.append() 实现的2. 字符串的格式:UTF-16...原创 2018-07-30 10:28:56 · 241 阅读 · 0 评论 -
Thread、ThreadLocal 关系
1. Thread 内存有 ThreadLocal 的 ThreadLocalMap 成员 /* ThreadLocal values pertaining to this thread. This map is maintained * by the ThreadLocal class. */ ThreadLocal.ThreadLocalMap threadLo...原创 2018-07-30 11:24:47 · 644 阅读 · 0 评论 -
理解 Unicode UTF-8 UTF-16 UTF-32 的要点
1. Unicode 只规定了字符与数字之间的映射关系,至于具体怎么由计算机表示,那是UTF的职责 Unicode 可以简单理解为一个电话本,电话号码就是数字表示,人名就是对应字符,二者能够相互映射 而电话号码具体你怎么记录(比如要不要加个密啥的,逆序书写等),Unicode 并不关心,那依赖 UTF 具体实现2. UTF 是实际负责如何存储字符的数字表示的,有不同...原创 2018-07-30 14:45:41 · 232 阅读 · 0 评论 -
Java 内存模型(JMM)
一、概述 Java 内存模型(Java Memory Model)描述了一组规则或规范,定义了 JVM 将变量存储到内存和从内存中取出变量这样的底层细节,值得注意的是,这里的变量指的是共享变量(实例字段、静态字段、数组对象元素),不包括线程私有变量(局部变量、方法参数),因为私有变量不会存在竞争关系。二、JMM 图解 在 JMM 中,线程直接...原创 2018-11-29 11:49:29 · 217 阅读 · 0 评论 -
[Java] 实现动态代理
1. 首先,定义一个接口,并实现该接口;2. 然后,定义一个handler,实现InvocationHandler接口,实现其invoke方法。 这里技巧是通过构造器传入目标代理对象(Object 类型),赋值给内部目标代理对象, 在实现invoke方法时,通过method.invoke(内部目标代理对象,args)实现真实调用;3. 使用代理时,先构造该handler原创 2016-08-21 00:18:00 · 465 阅读 · 0 评论