
Golang
shida_csdn
Keep Running ...
展开
-
Golang Template 使用自定义函数
Golang Template 使用自定义函数Go 语言内置的 text/template 支持自定义函数功能:// Funcs adds the elements of the argument map to the template's function map.// It must be called before the template is parsed.// It panics if a value in the map is not a function with appropria原创 2021-02-08 17:59:24 · 3278 阅读 · 0 评论 -
Kubernetes Operator 切换日志格式为 klog
本文仅作备忘在使用 KubeBuilder 构建 Operator 时,默认的日志格式是 zap 样式的,其日志初始化语句如下:ctrl.SetLogger(zap.Logger(true))有时候因为没有格式化打印日志的需要,可以修改日志输出形式,例如切换为 klog 格式,只需修改上述初始化语句为:import( "k8s.io/klog/klogr")...ctrl.SetLogger(klogr.New())......原创 2020-09-25 21:37:59 · 1414 阅读 · 0 评论 -
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 评论 -
Go 源码编译笔记
本文介绍如何编译 Go 源码1. 环境准备要编译 Go 源码,首先要准备编译环境鉴于 Go 目前也是通过 Go 语言编写的(自举),因此准备一个 Go 环境即可以 Ubuntu 18.04 为例:# cd /tmp # wget https://studygolang.com/dl/golang/go1.13.3.linux-amd64.tar.gz# tar zxvf go1.13...原创 2019-10-30 11:25:03 · 1954 阅读 · 0 评论 -
[leetcode-in-go] 0052-N-Queens II
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return the number of distinct solutions to the n-queens puzzl...原创 2019-07-17 21:57:48 · 184 阅读 · 0 评论 -
[leetcode-in-go] 0149-Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.记录一种 n^2 解法:掌握最大公约数的求法小数是不准确的,难以作为 key,需要使用字符串代替除法结果func max(a, b int) int { if a > b { r...原创 2019-07-23 23:42:12 · 209 阅读 · 0 评论 -
[leetcode-in-go] 0054-Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.Example 1:Input:[[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ]]Output: [1,2,3,6,9,8,7,4,5]Example...原创 2019-07-18 22:39:33 · 180 阅读 · 0 评论 -
vscode golang 开发环境搭建
下载安装 vscode见官网:https://code.visualstudio.com/#alt-downloads下载安装 golang见官网:https://golang.google.cn/dl/安装配置 vs code golang 插件ctrl + shift + x 调出扩展面板,输入 “go” 搜索,安装下图所示插件:注意该插件依赖很多 go tools,...原创 2019-07-18 18:32:18 · 517 阅读 · 0 评论 -
[leetcode-in-go] 0069-Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x, where x is guaranteed to be a non-negative integer.Since the return type is an integer, the decimal digits are truncated and only t...原创 2019-07-11 23:22:34 · 218 阅读 · 0 评论 -
[leetcode-in-go] 0050-Pow(x, n)
Implement pow(x, n), which calculates x raised to the power n (xn).Example 1:Input: 2.00000, 10Output: 1024.00000Example 2:Input: 2.10000, 3Output: 9.26100Example 3:Input: 2.00000, -2Output: ...原创 2019-07-11 22:30:35 · 195 阅读 · 0 评论 -
[leetcode-in-go] 0049- Group Anagrams
Given an array of strings, group anagrams together.Example:Input: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”],Output:[[“ate”,“eat”,“tea”],[“nat”,“tan”],[“bat”]]Note:All inputs will be in lowe...原创 2019-07-11 22:05:20 · 384 阅读 · 0 评论 -
[leetcode-in-go] 0048- Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the image in-place, which means you have to modify the input 2D matrix dire...原创 2019-07-10 23:09:03 · 254 阅读 · 0 评论 -
[leetcode-in-go] 0047-Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.Example:Input: [1,1,2]Output:[[1,1,2],[1,2,1],[2,1,1]]解题思路注意结果存储需要单独拷贝副本,放置被意外篡改切片因为扩容的...原创 2019-07-10 10:35:12 · 197 阅读 · 0 评论 -
[leetcode-in-go] 0009-Palindrome Number
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.Example 1:Input: 121Output: trueExample 2:Input: -121Output: falseExplanation...原创 2019-07-09 22:41:44 · 302 阅读 · 0 评论 -
[leetcode-in-go] 0051-N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.Each so...原创 2019-07-16 23:51:49 · 353 阅读 · 0 评论 -
[leetcode-in-go] 0055-Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you ...原创 2019-07-19 22:41:34 · 212 阅读 · 0 评论 -
字节顺序:大端法与小端法
一、什么是大端法、小端法假设变量 x 的类型为 int,位于地址 0x100 处,它的 16 进制值为 0x01234567,地址范围 0x100 ~ 0x103 的字节顺序依赖于机器的类型:大端法,就是按照地址从低到高的顺序,先存储数据高位,再存储数据低位;小端法,就是按照地址从低到高的顺序,先存储数据低位,再存储数据高位;小端法的存入顺序与我们平时对数字的书写顺序恰好相反。目前,In...原创 2019-09-01 16:03:04 · 1446 阅读 · 0 评论 -
proxy.golang.org: unexpected status 410 Gone
问题背景:当我们设置了 GOPROXY=https://proxy.golang.org,使用 Go module 管理依赖,有时会出现依赖找不到的情况(报错类似这样):go: github.com/golangci/ineffassign@v0.0.0-20180808204949-42439a7714cc: unexpected status (https://proxy.golang....原创 2019-08-24 18:58:27 · 3450 阅读 · 2 评论 -
[leetcode-in-go] 0151-Reverse Words in a String
Given an input string, reverse the string word by word.Example 1:Input: “the sky is blue”Output: “blue is sky the”Example 2:Input: " hello world! "Output: “world! hello”Explanation: Your reve...原创 2019-07-21 23:26:16 · 217 阅读 · 0 评论 -
头条面试题-统计有序数组里平方和的数目
给你一个有序整数数组,数组中的数可以是正数、负数、零,请实现一个函数,这个函数返回一个整数:返回这个数组所有数的平方值中有多少种不同的取值。举例:nums = {-1,1,1,1},那么你应该返回的是:1。因为这个数组所有数的平方取值都是1,只有一种取值nums = {-1,0,1,2,3}你应该返回4,因为nums数组所有元素的平方值一共4种取值:1,0,4,9解题思路双...原创 2019-07-21 12:12:02 · 569 阅读 · 0 评论 -
[leetcode-in-go] 0146-LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.get(key) - Get the value (will always be positive) of the key if the...原创 2019-07-21 11:05:15 · 212 阅读 · 0 评论 -
[leetcode-in-go] 0058-Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ’ ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defined...原创 2019-07-20 17:46:27 · 200 阅读 · 0 评论 -
[leetcode-in-go] 0053-Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Example:Input: [-2,1,-3,4,-1,2,1,-5,4],Output: 6Explanation:...原创 2019-07-17 23:07:55 · 274 阅读 · 0 评论 -
[leetcode-in-go] 0057-Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Example...原创 2019-07-20 17:29:06 · 227 阅读 · 0 评论 -
[leetcode-in-go] 0056-Merge Intervals
Given a collection of intervals, merge all overlapping intervals.Example 1:Input: [[1,3],[2,6],[8,10],[15,18]]Output: [[1,6],[8,10],[15,18]]Explanation: Since intervals [1,3] and [2,6] overlaps, m...原创 2019-07-20 15:30:48 · 192 阅读 · 0 评论 -
[leetcode-in-go] 0046-Permutations
Given a collection of distinct integers, return all possible permutations.Example:Input: [1,2,3]Output:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]解题思路递归(cur + remain, *result)特别注意...原创 2019-07-08 23:59:07 · 175 阅读 · 0 评论 -
[leetcode-in-go] 0008-String to Integer (atoi)
Implement atoi which converts a string to an integer.The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this...原创 2019-07-08 21:33:29 · 192 阅读 · 0 评论 -
[leetcode-in-go] 0006-ZigZag Conversion
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I I G...原创 2019-07-02 23:24:41 · 162 阅读 · 0 评论 -
Golang 反射牛刀小试
一、引言 在学习 Java 时,反射一直是个重点掌握内容,ORM 等框架的实现更是与反射息息相关; Go 语言同样提供了对反射的支持,json 序列化库就是基于反射来工作的; 基于反射,我们不仅能够获得变量的类型信息,而且能够动态修改变量内部的字段值。二、反射的使用 我们就使用简单的代码,最快地验证下反射功能package ...原创 2019-03-01 11:10:48 · 221 阅读 · 0 评论 -
Helm 源码编译
一、背景介绍 helm 是重要的 k8s 包管理工具,其源码托管在:https://github.com/helm/helm 本文介绍如何基于 ubuntu 18.04 环境,源码编译 helm二、编译步骤2.1 安装 golang、git # apt install golang git -y2.2 配置 GOPATH、PATH 环境变...原创 2019-01-22 09:54:04 · 1338 阅读 · 0 评论 -
godep 安装使用介绍
一、前言 godep 是解决包依赖的管理工具,也是目前最主流的一种 golang 项目包管理工具 kubernetes 等主流项目都在使用,相比直接 vendor 方式, godep 在其基础上增加了版本控制的概念二、安装 官方项目 github 地址:https://github.com/tools/godep 安装比较...原创 2019-01-15 10:39:55 · 3690 阅读 · 0 评论 -
QuickSelect 查找第 K 小的元素
Quick select 算法通常用来在未排序的数组中寻找第 k 小/第 k 大的元素。其方法类似于 Quick sort。本质上是通过多次快速排序,当某次快速排序的枢纽元素恰好下标为 k-1 时,结束查找~package mainimport "fmt"func core(nums []int, k, start, end int) int { left, right := ...原创 2018-12-19 11:09:37 · 782 阅读 · 0 评论 -
寻找和为定值的两个数
题目描述输入一个数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。要求时间复杂度是O(N)。如果有多对数字的和等于输入的数字,输出任意一对即可。例如输入数组1、2、4、7、11、15和数字15。由于4+11=15,因此输出4和11。解法总结不论原序列是有序还是无序,解决这类题有以下三种办法:1、二分(若无序,先排序后二分),时间复杂度总为O(N log ...原创 2018-12-20 14:33:17 · 206 阅读 · 0 评论 -
golang 最大子数组问题
所谓最大子数组就是连续的若干数组元素,如果其和是最大的,那么这个子数组就称为该数组的最大子数组。最大子数组是很多问题的抽象,比如购买股票。如果把相邻两天的股价之差作为数组元素,那么求在连续的某个时间段内买入股票的最佳时间和卖出股票的最佳时间就可以抽象为计算最大子数组的问题。时间复杂度为 n 的解法如下:package mainimport ( "fmt" "math")//...原创 2018-12-06 14:20:04 · 388 阅读 · 0 评论 -
Golang 内存模型
内存模型的目的是为了定义清楚变量的读写在不同执行体里的可见性在 Golang 中,遵循的内存可见性原则主要包括如下几类:关于初始化:1. 如果 package p 引用了 package q,q 的 init() 方法 happens-before p2. main.main() 方法 happens-after 所有 package 的 init() 方法结束关于 G...原创 2018-12-12 20:58:03 · 1046 阅读 · 0 评论 -
fork/exec /bin/sh: operation not permitted
我在 ubuntu 18.04 系统下使用 go 语言执行 sh 命令,设置 uid、gid 报错 cmd := exec.Command("sh") cmd.SysProcAttr = &syscall.SysProcAttr{ Cloneflags: syscall.CLONE_NEWUTS | syscall.CLONE_NEWIPC | syscall.CLONE_NE...原创 2018-11-30 15:15:04 · 6078 阅读 · 2 评论 -
Golang Socket 编程实例
服务端程序:package mainimport ( "net" "fmt" "strings")func handle(conn net.Conn) { defer conn.Close() buf := make([]byte, 2048) for { n, err := conn.Read(buf) if err != nil { fmt.Print...原创 2018-11-05 11:43:08 · 461 阅读 · 0 评论 -
Go 类型断言 .(type)
关键词:interface,switch类型断言 x.(T) 其实就是判断 T 是否实现了 x 接口,如果实现了,就把 x 接口类型具体化为 T 类型;而 x.(type) 这种方式的类型断言,就只能和 switch 搭配使用,因为它需要和多种类型比较判断,以确定其具体类型。x.(type) 使用示例:func checkType(args ...interface{}) { f...原创 2019-03-27 12:36:30 · 5285 阅读 · 0 评论 -
Golang unsafe 指针转换与内存操作
Golang 提供了 unsafe 包,让我们能够直接操作指定内存地址的内存。通过 unsafe.Pointer() 函数,我们能够获取变量的内存地址,本质上这是个整数。但 Pointer 不支持运算,如果要在内存地址上进行加减运算,需要将其转为 uintptr 类型。下面我们尝试读取切片地址,并通过内存操作遍历其内容:package mainimport "fmt&am原创 2019-03-04 11:05:10 · 3349 阅读 · 0 评论 -
[leetcode-in-go] 0007-Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3:Input: 120Output: 21Note:Assume we are dealing with an ...原创 2019-07-07 18:32:45 · 159 阅读 · 0 评论