How fast is doing a go func() for a block of code ?
<p>Is there a performance hit from starting goroutines like this ? Is there a comparable Thread executor (in Java) i.e. goroutine executor ?</p> <hr/>**评论:**<br/><br/>tylermumford: <pre><blockquote> <p>Is there a performance hit from starting goroutines like this ?</p> </blockquote> <p&...阅读全文
Go语言Hello world(GOPATH和Go Module版)
本文是「vangoleo的Go语言学习笔记」系列文章之一。官网: http://www.vangoleo.com/go/go-hello-world-02/ 往期回顾: Go语言入门-你好,Go语言 Go语言入门-Hello World(Go Playground版) 上一篇文章Go语言入门:Hello world中,我们在Golang Playground中体验了Go语言,编写并运行了一个简单的Hello World,相信大家对Go语言的语法有了一些了解。 学习一种编程语言,除了基本的语法,更重要的是要了解如何在实际的工程中使用它。本文中,我会和大家一起,从零开始,安装Go语言,配置环境,安装IDE,开发一个Hello World程序。这个Hello World版我会编写两个版本:GOPA...阅读全文
Job in Sydney: Fullstack Go Web Developer
<p>Digital reputation based start-up in Sydney Australia, is ready to take on the world! We need a third full-stack Senior Go Web Developer.</p> <p>Our ideal candidate will have proven full-stack Go development and also be comfortable in SQL (Postgres) and Ruby on Rails. Experience in PHP, NodeJS or Python is an advantage.</p&...阅读全文
Going to write my webapp in Go instead of Node. Is it a good choice?
<p>I'm a front-end developer but I have coded with a few other languages. Did some PHP projects in the past (with laravel), created a desktop app with Java and I use a bit of C in college. Also did a website with ruby and rails.</p> <p>I've been working only with javascript for a couple years at least in the front...阅读全文
240.搜索二维矩阵 II
题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具有以下特性: 每行的元素从左到右升序排列。 每列的元素从上到下升序排列。 示例: 现有矩阵 matrix 如下: [ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30] ] 给定 target = 5,返回 true。 给定 target = 20,返回 false。 思路 1.根据此二维矩阵给出的性质,可以从二维数组的右上角开始搜索,若target大于当前元素,我们的检索方向就向下,若小于,我们的检索方向就向左,若检索出界,还没找到结果,直...阅读全文
duck typing
像鸭子走路,像鸭子叫,那么就是鸭子 描述事物外部行为而非内部结构 严格说go属于结构化类型系统,类似duck typing python c++ 中duck typing (只要retriever 具有get方法即可)但是编译运行才知道错误 def download(retriever r): r.get("www.baidu.com") java 中传入参数必须要实现某个接口,不是duck typing 接口定义 download (使用者) retriever(使用者) golang中接口由使用者定义 typpe assertion n, p := reader.Shape() point := p.(*shp.Point) x := point.X y := point.Y 接口变量里...阅读全文
Using maps, pointers and arrays
<p>Hi guys. I've hit bit of a "how should this be done in golang" wall. </p> <p>What I have is a <code>items := map[string][]*someType</code> </p> <p>and a little later a check </p> <pre><code>Add item i if key exists i := new(someType) if cache, ok := items[&...阅读全文
How is no semicolon achieved in GO language(grammar speciifcally)?
<p>Ok so i know go automatically inserts a semicolon if the last token in a input line is an identifier,a basic literal such as a number or string constant, or one of the tokens : break continue fallthrough return ++ -- ) }. Is this implemented in lexers grammar or parsers grammar?(i know lexer breaks the input into tokens). I want to see the...阅读全文
polymorphism madness
<p>I wanted to post this to <a href="/r/programming" rel="nofollow">/r/programming</a> but they only accept links, and I could care less about blogging.</p> <p>First, a short preface. I write a lot of Go code. Not all of it is public and yet very little of it is paid for. Frankly, a number of my hobbie...阅读全文
Is Go 2 actually happening? If so, when will development start?
<hr/>**评论:**<br/><br/>lstokeworth: <pre><p>Go 2 is <a href="http://www.thedynamiccommunicator.com/increase-meeting-productivity-the-idea-parking-lot/">an idea parking lot</a>. There are no plans to start development on Go 2.</p></pre>thockin: <pre><p>It does sort of kill me ...阅读全文
Safe to share data between goroutines without explicit locks?
<p>If I want to share a big tree data structure between goroutines, what happens if two gophers try to reassign a pointer at the same time? Append to a slice? Should I always protect these mutations with explicit locks, or are some operations safe?</p> <p>Does Go at least protect against corruption, in the case of bit-level write...阅读全文
Is Go suitable for building payment gateway backend?
<p>I have seen Java, .net technologies are used in this field. But never heard of Go. I want to know what kind of advantages and drawbacks may be? </p> <hr/>**评论:**<br/><br/>dlsniper: <pre><p>Considering there's a bank, Monzo in UK, building their whole backend in Go, then yes, Go probably is ok.&l...阅读全文
The creator of node stopped building the language because he knew go was better.
<p><a href="http://pca.st/lT7W">http://pca.st/lT7W</a></p> <hr/>**评论:**<br/><br/>Fwippy: <pre><p>Node isn't a language.</p></pre>evilmaus: <pre><p>But it is an attempt to bring JavaScript out of the browser.</p></pre>tonymonmaster: <pre>&...阅读全文
Generics: specific use cases
<p>There's been a <a href="https://www.reddit.com/r/golang/comments/6n2g8w/go2_is_real/">lot</a> <a href="https://www.reddit.com/r/golang/comments/6n31hj/toward_go_2/">of</a> <a href="https://www.reddit.com/r/golang/comments/6na5sy/it_came_to_them_with_a_message/">discussion</a&...阅读全文
Why Go is so popular for web-development?
<p>Hello, guys. I've noticed a lot of people switch from traditional web stack to Go. What's the main purpose?</p> <p>There are several advantages if you think of Go <strong>as a reasonable replacement for C++ or Java.</strong> But Go also seems to have a growing popularity <strong>in web-develop...阅读全文
why is golang interface internally an int
<p>This is totally different from java. I've read many articles, but seems like none of them dealing with the design purpose. Is there a TL;DR version of why is this?</p> <hr/>**评论:**<br/><br/>Sythe2o0: <pre><p>Where did you get the impression that an interface was an int? As a datatype, an interfa...阅读全文
JAVA多线程使用场景和注意事项简版
我曾经对自己的小弟说,如果你实在搞不清楚什么时候用HashMap,什么时候用ConcurrentHashMap,那么就用后者,你的代码bug会很少。 他问我:ConcurrentHashMap是什么? -.- 编程不是炫技。大多数情况下,怎么把代码写简单,才是能力。 多线程生来就是复杂的,也是容易出错的。一些难以理解的概念,要规避。本文不讲基础知识,因为你手里就有jdk的源码。 线程 Thread 第一类就是Thread类。大家都知道有两种实现方式。第一可以继承Thread覆盖它的run方法;第二种是实现Runnable接口,实现它的run方法;而第三种创建线程的方法,就是通过线程池。 我们的具体代码实现,就放在run方法中。 我们关注两种情况。一个是线程退出条件,一个是异常处理情况。 线程...阅读全文
构建乘积数组
题目描述 给定一个数组 A[0,1,…,n-1],请构建一个数组 B[0,1,…,n-1],其中 B 中的元素 B[i]=A[0]×A[1]×…×A[i-1]×A[i+1]×…×A[n-1]。不能使用除法。 示例 输入: [1,2,3,4,5] 输出: [120,60,40,30,24] 思路 1.可以从题目给出的公式中,推导出一幅图。 根据图片的内容,可以通过计算“三角形”的结果,推导出最终元素的结果。 3.三角形主要包括,上半部和下半部,具体计算详情,可以参看代码。 Java代码实现 class Solution { public int[] constructArr(int[] a) { if(a.length == 0){ return a; } int[] res = new in...阅读全文
Learning the core Go libraries
<p>Hello! I'm new to Golang and this subreddit. I have been learning the basic Go syntax and structure recently, and since I have found it rather intuitive, I've decided to take the plunge and truly try and learn as much Go as I can this summer (aka, you'll probably see me more often in this sub in the future...). I ca...阅读全文
Is interface{} acceptable? Discuss.
<p>If it's not and sacrificing type safety, then why is it there? If it's there, why using it is considered bad.? Isn't it just like an "any" type in many languages (except there is a <code>nil</code> in Go). I've heard arguments about this being a bad practices only from the communi...阅读全文
Why golang refuse to provide goroutine id, then they hack their own way to do it?
<p>This is aweful: <a href="https://github.com/golang/net/blob/master/http2/gotrack.go#L51">https://github.com/golang/net/blob/master/http2/gotrack.go#L51</a></p> <p>Ary they enjoying this?</p> <hr/>**评论:**<br/><br/>kodablah: <pre><p>A madlib: Why golang refuse to provide &a...阅读全文
Logging per category like log4j
<p>Hi there, I have been searching a lot, but I haven't found any logging library that allows me to set different logging levels for different packages. I mean. For example, I want to set my program level to debug, as I develop it, but perhaps, I would also want to a library/package that I use, that of course would use the same loggin...阅读全文
Problem parsing xml file
<p>I have the following xml file: <?xml version="1.0" encoding="utf-8" standalone="yes" ?></p> <pre><code><java-update-map version="1.0"> <mapping> <version>1.8.0_51</version> ...阅读全文
How do you practice good design?
<p>Hey there,</p> <p>I am aware that this is probably not the 100 percent correct subreddit for this question but since I am currently working mostly with go and also since my current goal is to improve my design of Go programs I wanted to start here.</p> <p>So I was watching this wonderful talk <a href="https...阅读全文
I'm confused, how do generics work in Go ?
<p>I haven't found much tutorials about generic Go programming, could you guys explain for me how that works ?</p> <hr/>**评论:**<br/><br/>jlund3: <pre><p>My first gut reaction was: Go doesn't have generics. We use interfaces for generic algorithms and type assertion/reflection for generic dat...阅读全文
老虞学GoLang笔记-常量
开发十年,就只剩下这套Java开发体系了 >>> 常量 常量和C#中的概念相同,在编译期被创建。因为在编译期必须确定其值,因此在声明常量时有一些限制。 其类型必须是:数值、字符串、布尔值 表达式必须是在编译期可计算的 声明常量的同时必须进行初始化,其值不可再次修改 Doc http://golang.org/doc/go_spec.html#Constants http://golang.org/doc/go_spec.html#Constant_expressions http://golang.org/doc/go_spec.html#Constant_declarations http://golang.org//doc/go_spec.html#Iota 语法 const关键字用于声...阅读全文
2019.01.27
内建变量类型 内置变量类型bool, string(u)int, (u)int8, (u)int16, (u)int32, (u)int64, uintptrbyte, runefloat32, float64, complex64, complex128(u)是unsigned代编有符号或无符号go与其他不同的类型1、uintptr go语言的指针类型2、rune 其实rune是char类型,而且是char32位3、complex 复数类型。Go语言类型转化func triangle() { var a ,b int = 3,4 var c int //go语言没有隐式强制类型转换,必须显示强制类型转换 c = int(math.Sqrt(float64(a*a+b*b))) fmt.Pr...阅读全文
利用golang优雅的实现单实例
平时编写代码过程中,经常会遇到对于全局角度只需运行一次的代码,比如全局初始化操作,设计模式中的单例模式。针对单例模式,java中又出现了饿汉模式、懒汉模式,再配合synchronized同步关键字来实现。其目的无非就是将对象只初始化一次,而且最好保证在用到的时候再进行初始化,以避免初始化太早浪费资源,或者两次初始化破坏单例模式的实例唯一性。 Go语言的sync包中提供了一个Once类型来保证全局的唯一性操作,其通过Do(f func())方法来实现,即使 f 函数发生变化,其也不会被执行,下面我们来看一个小例子: package main import ( "fmt" "sync" "time" ) var once sync.Once func main() { //once循环调用fir...阅读全文
is Go good choice as first programming language?
<hr/>**评论:**<br/><br/>sh41: <pre><p>I personally think it's one of the best programming languages to start with. I wish I could've started with it first. The only thing it won't teach you is frustration of using other less pleasant languages, so you might not have enough context to appreciate so...阅读全文
Question about interfaces
<p>Hey guys<br/> coming from a java world I try to grasp the concepts of go's interfaces. As far as I understood, the interfaces are implemented implicitly if all methods are satisfied. But isn't one of the ideas behind interfaces to force the implementation? Like when I have a huge'ish interface and I miss to im...阅读全文
naive "generics" proposals
<p>I work daily in Java (sad, but true), and appreciate there are some cases where generics are really handy. Most examples, to my eye, are sinful. Here are a couple sinful examples:</p> <pre><code>Map<String, List<Map<String, String>>> public class FiniteStateMachine<S exten...阅读全文
树的子结构
题目描述 输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构) 思路 1.递归比较两个数的结构。 2.比较A树和B树当前的结点,若A树和B树的值相等,则继续比较它们的左右子树;若不相等,则拿A树的左子树和右子树进行同样的过程。 3.当B树遍历到了空结点,说明B是A的子结构;否则不是A的子结构。 Java代码实现 public boolean HasSubtree(TreeNode root1,TreeNode root2) { if(root1 == null || root2 == null){ return false; } boolean flag = judge(root1,root2); if(!flag){ flag = HasSubtree...阅读全文
二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像。 输入描述 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5 思路 使用递归算法求解。 如果传入的结点不为null,则将其左右孩子进行交换,然后递归处理其左右孩子即可。 Java代码实现 public class Solution { public void Mirror(TreeNode root) { if(root != null){ TreeNode temp = root.left; root.left = root.right; root.right = temp; Mirror(root.left); Mirror(root...阅读全文
Error handling with streaming data
<p>I'm working on a personal project with Go, and I'm relatively new to it. Part of what I'm doing involves repeatedly reading data out of a Reader in 2-8 byte chunks. The Go way of handling errors (as far as I've found) is to use a pattern like this:</p> <pre><code>content := make([]byte, le...阅读全文
AWS Lambda to Fully Support Go
<p>Just announced at re:Invent, Go becomes a first class citizen on Lambda.</p> <hr/>**评论:**<br/><br/>emostafa: <pre><p>Do you guys find it weird that Google Cloud Functions doesn't support Go ? only nodejs at the moment.</p></pre>chuyskywalker: <pre><p>GCF is really behind o...阅读全文
How can Go make you a master of web development
<p>I have been developing applications in Go for a year now, and I really want to thank Google and the community to make such a great language and countless useful modules. I have learned so much and I would like to share what I have learned so far.</p> <p><a href="https://medium.com/@houyubing24/how-can-go-make-you-a-mast...阅读全文
What's a good project to do to learn Go?
<p>I have some downtime at work and I can spend my time learning what I want. Go has been on the list for a while and I'd really like to learn it and possibly use it in real projects.</p> <p>Now, I could make a web backend with an MVC architecture, make some endpoints etc... But that's kinda boring as it would jus...阅读全文
Go web developer jobs?
<p>I'm a 15 year old that's currently learning Go. I love it. Seriously, so much better than Node and Java for me. The thing is, when I look for jobs (well really I'd be looking for an internship), I have like 5 results for Go, and hundreds for Java, JavaScript, etc. My question is this: is Go a language that you can m...阅读全文
Preferred Golang alternatives to traditional object-oriented patterns?
<p>A lot of programming teaching material these days assumes that you are using a traditional object-oriented language (example: Java) with traditional object-oriented patterns (example: inheritance) to structure and model your program. In the case of Go, it is a different language with different building blocks. Then, what are the preferred ...阅读全文
Decisions around usage of nil in go?
<p>I have been using go for quite awhile, and haven't really given much thought to the way that the usage of nil is used versus other languages I have used (Java/C#/Node). I have always kind of accepted it for what it was.</p> <p>However, as golang makes more of an appearance in our stack, I find myself questioning why th...阅读全文
golang concurrency with real life examples
<p>Goroutine works like the thread in java or any other multi-threading programming language, though the internal concept is different. Goroutine is more lightweight than processor thread. It takes little memory (on average 4~8 KB) than regular thread. Goroutines are managed by thread manager in runtime.</p> <p><a href="ht...阅读全文
Android Development?
<p>I am brand new to golang. I am enjoying everything I have learned about it so far. However I cant seem to find a clear answer on whether this is a legitimate way to design Android Apps or if I should stick to Java for this. I see alot of posts about how it's coming, but a lot of them are from like 2 years ago. Anyone here use golan...阅读全文
What is the proper way of throwing errors in go code?
<p>So I was making a function that interfaces with a database and has errors implemented like this:</p> <pre><code> // A function to create a new post func CreatePost(title string, content string) (error) { // Open a db connection and check errors db, dbErr := InitDb() defer db.Close() if dbErr != nil { ...阅读全文
Best site or place to start Golang
<p>Hey, I was wondering if any of you know what are some of the best sites or places to start learning Golang, thanks.</p> <hr/>**评论:**<br/><br/>gobdgobd: <pre><p>The tour of go is great!</p> <p><a href="https://tour.golang.org/welcome/1" rel="nofollow">https://tour.golang....阅读全文
[noobish question] struct pointers?
<p>Hi everyone!<br/> noob question here as I'm taking the Go tour on the website. My main languages are Python and Java (although I know a bit of Rust).<br/> Now, I understand how to use pointers, but as I was going through the pointers section of the tour I was wondering:<br/> if we can initialize structs this way ...阅读全文
Thinking about using Go as a composite service.
<p>I have a composite service aggregating data from other microservices, I was wondering how go would handle thousands of aggregations.</p> <hr/>**评论:**<br/><br/>xy8_t_rel_: <pre><p>define composite service.</p></pre>lolomfgkthxbai: <pre><p>Is that the buzzword of the year? Composite...阅读全文
老虞学GoLang笔记-常量
开发十年,就只剩下这套Java开发体系了 >>> 常量 常量和C#中的概念相同,在编译期被创建。因为在编译期必须确定其值,因此在声明常量时有一些限制。 其类型必须是:数值、字符串、布尔值 表达式必须是在编译期可计算的 声明常量的同时必须进行初始化,其值不可再次修改 Doc http://golang.org/doc/go_spec.html#Constants http://golang.org/doc/go_spec.html#Constant_expressions http://golang.org/doc/go_spec.html#Constant_declarations http://golang.org//doc/go_spec.html#Iota 语法 const关键字用于声...阅读全文
Go语言入门:Hello world
本文是「vangoleo的Go语言学习笔记」系列文章之一。官网: http://www.vangoleo.com/go/go-hello-world/ 在上一篇文章你好,Go语言中,我们对Go语言的历史和特点有了认识。接下来,我们就正式进入Go的学习了。按照大部分编程语言的惯例,我们以经典的“Hello World”开始Go语言的学习之旅。 经典的“Hello World”案例来自于1978年出版的C语言圣经
leetcode_55
Golang: 思路:这题挺有意思的,但并不是在于它的难度上,还是解题的思路上。目前我实现的是思路一:从前往后对数组做处理,但实现的效率极低,大概时间复杂度15%,空间复杂度15%左右。所以更高效的应该是思路二:使用迭代去实现一种类似回溯的方法,对数组进行从后往前的处理。后面我实现了思路二,程序效率如下: 实现效果 下面着重讲下思路二,这里有个前提,即这个问题是可以分解成子问题的。举例分析,我们从arr[i]可以到达终点,那么能否到达终点这个问题就变成了能否到达arr[i]这个问题了。当一个问题可以被分解,那么迭代就有了可行性。 Emmm,对于陌生的题,我会先选择最熟悉的JAVA去写,后面才会用Go去复写,所以上面的提交图是JAVA的。下面给出Go语言实现的代码: func canJump(...阅读全文