Go语言中文网 为您找到相关结果 1394

Golang基础(六) | 接口

Interface类型 简单的说,interface是一组method签名的组合,我们通过interface来定义对象的一组行为。interface类型定义了一组方法,如果某个对象实现了某个接口的所有方法,则此对象就实现了此接口。详细的语法参考下面这个例子。 type Human struct { name string age int phone string } type Student struct { Human //匿名字段Human school string loan float32 } type Employee struct { Human //匿名字段Human company string money float32 } //Human对象实现Sayhi方法 func ...阅读全文

博文 2020-03-24 22:32:49 youzhihua

How to properly do templating with multiple templates and multiple pipelines

<p>The usual examples of the template package I see is a struct with 2 simple strings which get called through {{.First}} {{.Last}} in html. Really obvious stuff.</p> <p>Now what do I do if I have a complex template? Let&#39;s assume I have a table in my index.html. I already splitted that table out as a new template with {{...阅读全文

资源 2017-02-04 12:00:11 agolangf

Go的50坑:新Golang开发者要注意的陷阱、技巧和常见错误[1]

Go是一门简单有趣的语言,但与其他语言类似,它会有一些技巧。。。这些技巧的绝大部分并不是Go的缺陷造成的。如果你以前使用的是其他语言,那么这其中的有些错误就是很自然的陷阱。其它的是由错误的假设和缺少细节造成的。 如果你花时间学习这门语言,阅读官方说明、wiki、邮件列表讨论、大量的优秀博文和Rob Pike的展示,以及源代码,这些技巧中的绝大多数都是显而易见的。尽管不是每个人都是以这种方式开始学习的,但也没关系。如果你是Go语言新人,那么这里的信息将会节约你大量的调试代码的时间。 目录 初级篇 开大括号不能放在单独的一行 未使用的变量 未使用的Imports 简式的变量声明仅可以在函数内部使用 使用简式声明重复声明变量 偶然的变量隐藏Accidental Variable Shadowing...阅读全文

博文 2020-05-19 10:36:25 开发者阿俊

golang中slice处理遇到的一个关于引用的坑

前两天在解扫地机器人算法的问题时,遇到一个坑 部分代码如下: func move2(startPoint Point) [][]Point { allFootPrint := [][]Point{{startPoint}} for i := 0; i < N; i++ { allNewFootPrint := make([][]Point, 0) for len(allFootPrint) > 0 { curFootPrint := allFootPrint[len(allFootPrint)-1] allFootPrint = allFootPrint[:len(allFootPrint)-1] last := curFootPrint[len(curFootPrint)-1] for _...阅读全文

博文 2019-08-22 15:33:51 ustb80

The conventional FizzBuzz problem in go Lang using Maps.

<p>I am learning go and was curious on how to use go maps to solve the conventional fizzbuzz problem, i.e. print number from say, 1-100 and multiples of 3 &amp; 5 get printed fizz and buzz respectively. I looked over a bunch of solutions in different languages but somehow can&#39;t produce the same results in go. Thanks in advance.<...阅读全文

Was hoping to get some critique on my first go library!

<p>Hey all. I&#39;m a hobbyist programmer at best and getting back into programming I decided to tackle Go! I am having a blast with it so far and I&#39;m currently working on sort of a managed job queue. Where you can set up jobs, send them to the queue, and the queue will fire up a MaxActiveJobs number of jobs and wait for them to f...阅读全文

Golang | 高级数据类型

一、数组 数组作为函数参数,传值的; 只有长度和类型相同,才是同一类型,才可以相互赋值; var arr = [10]int{1, 2, 3}//声明长度才是数组,没声明长度的是切片 //切片可以append,数组不可以 //[]int 和 [10]int是不能相互赋值的。 复制代码二、切片 切片是引用类型, 什么是引用类型? "引用类型" 有两个特征:1、多个变量引用一块内存数据,不创建变量的副本,2、修改任意变量的数据,其它变量可见。 1、slice内存结构 type SliceHeader struct { Data uintptr Len int Cap int } //在64位架构的机器上,一个切片需要24字节的内存:指针字段需要8字节,长度和容量字段分别需要8字节。 //可以看出...阅读全文

博文 2020-04-06 00:34:22 _Liu_

[Beginner]What is the idiomatic way in golang to code against another packages's interface?

<p>This might be a dumb question, but I have the following code</p> <pre><code>package SomeCleverDistributedAlgorithm type AbstractServer interface { doSomething(string) interface{} } type AlgorithmStruct struct { } func NewSomeCleverDistributedAlgorithm(serverList []AbstractServer) *AlgorithmStruct { return &a...阅读全文

资源 2017-04-03 01:00:10 polaris

Object equality

<p>I&#39;m trying to implement a simple <code>assertEquals</code> function. I&#39;m at the point when I don&#39;t understand how object equality works. Please have a look at this example (simplified for brevity): <a href="https://play.golang.org/p/bFv6KJcxcr8" rel="nofollow">https://play.golang.or...阅读全文

资源 2018-03-04 07:30:10 blov

Understanding slices. A question on the use of append() to insert a value into a slice.

<p>I am attempting to insert an element into an array at a particular index. To do this I split the initial array in two, creating two slices. The &#34;before&#34; slice is the array up to, but excluding, the index. The &#34;after&#34; slice contains the index and the remainder of the array. I then used append to add my new va...阅读全文

资源 2017-04-22 07:00:03 agolangf

why slice of raw type, e.g., byte is not supported as map keys but array of raw types is ok.

<p>I read this article: . it says</p> <p>Key types</p> <blockquote> <p>As mentioned earlier, map keys may be of any type that is comparable. The language spec defines this precisely, but in short, comparable types are boolean, numeric, string, pointer, channel, and interface types, and structs or arrays that co...阅读全文

资源 2018-02-24 06:30:02 xuanbao

第九章 九析带你轻松完爆 go - 切片 slice

系列文章:总目录索引:九析带你轻松完爆 go 系列教程目录1 前言2 切片的声明3 切片的初始化 3.1 先声明后初始化 3.2 声明并初始化4 切片和数组的关系 4.1 数组转化为切片 4.2 切片的长度 4.3 切片的容量 4.4 切片的切片1 前言 如果你对博客有任何疑问或者想更深入学习 go,请加微信群,我们一起进步:2 切片的声明 切片在 go 语言中是一种特殊的数据类型,你不要觉得它难学,你只需要将它看成一种特殊的数组类型即可。它跟数组区别在于,数组声明时需要指定长度,但切片并不指定长度。如下所示:package mainimport "fmt"func main() { var slice []int # 切片 var arr [2]int # 数组 fmt.Println(s...阅读全文

博文 2020-03-01 23:33:11 九析

Go 语言学习笔记 -第4章复合数据类型

Go 语言学习笔记 -第4章 [toc] 复合数据类型 数组 Golang中操作数组或者序列化数据需要用到slice,程序中写作“[]" slice 指向数组的值,并且同时包含了长度信息 package main import "fmt" func main() { // list := []int{1, 2, 3, 4} list := [...]int{1, 2, 3, 4} fmt.Println(list) fmt.Printf("Type %T\n", list) for i := 0; i < len(list); i++ { fmt.Printf("list[%d]=%d\n", i, list[i]) } //重新切片s[low:high], low->(high-1) fm...阅读全文

博文 2020-01-09 15:32:41 Mark110

Why is it faster to manipulate elements of a pointer to a slice?

<p>In this test program, I have 2 functions that manipulate elements of a slice. One function just gets passed the slice, the other gets passed the address of the slice. On my machine (x86-64 intel i7 6700k) it is consistently faster to manipulate elements of a pointer to a slice (you can switch up the order of chg() and chgPtr() to see that ...阅读全文

资源 2017-04-30 16:00:24 polaris

ARTS 第1周 LeetCode 78 Subsets | Golang 二维 slice 注意事项 | Go 官网文档

ARTS ARTS 是陈浩(网名”左耳朵耗子“)在极客时间专栏里发起的一个活动,目的是通过分享的方式来坚持学习。 每人每周写一个 ARTS:Algorithm 是一道算法题,Review 是读一篇英文文章,Technique/Tips 是分享一个小技术,Share 是分享一个观点。 算法 LeetCode 78 Subsets & 90 Subsets II 先看一下题目要求。 78.Subsets Given a set of **distinct** integers, _nums_, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets...阅读全文

博文 2020-05-18 10:32:45 casmo澎湃哥

Go 每日一库之 go-flags

简介 在上一篇文章中,我们介绍了flag库。flag库是用于解析命令行选项的。但是flag有几个缺点: 不显示支持短选项。当然上一篇文章中也提到过可以通过将两个选项共享同一个变量迂回实现,但写起来比较繁琐; 选项变量的定义比较繁琐,每个选项都需要根据类型调用对应的Type或TypeVar函数; 默认只支持有限的数据类型,当前只有基本类型bool/int/uint/string和time.Duration; 为了解决这些问题,出现了不少第三方解析命令行选项的库,今天的主角go-flags就是其中一个。第一次看到go-flags库是在阅读pgweb源码的时候。 go-flags提供了比标准库flag更多的选项。它利用结构标签(struct tag)和反射提供了一个方便、简洁的接口。它除了基本的功...阅读全文

博文 2020-01-13 21:32:41 darjun

重新认识Go的Slice

开篇语 大多数时候我们都忘记了或者压根不知道slice是怎么工作的。大多数时候我们只是把slice当做动态数组来用。通过重新认识slice,我们可以一定程度上避免掉入slice的陷阱,并且更好的使用它。 参考资料有: Effective Go Go Slices: usage and internal 本文重点是代码例子,边动手边学习 回归本元: 什么是数组? Go中的数组(array)是一个固定大小的、单一类型的一个序列。 创建数组需要两个参数:size和type。 Array的size是类型的一部分 x := [5]int{1, 2, 3} y := [5]int{3, 2, 1} z := [5]int{1, 2, 3} fmt.Printf("x == y: %v\\n", x ==...阅读全文

博文 2020-03-23 19:32:46 麻瓜镇

[Beginner] What is the difference between passing an slice containing the address pointer of a struct vs a slice containing the struct to a function

<p>Consider the following <a href="https://play.golang.org/p/VdSJYwvjFV" rel="nofollow">playground</a></p> <p>We see that passing <code>[]*Vertex</code> and <code>[]Vertex</code> both change the underlying struct in the slice.</p> <p>Is both <code>[]*Vertex<...阅读全文

资源 2017-07-02 17:00:06 xuanbao

Golang 复合数据类型:切片

切片(slice) 切片的底层是数组实现的,可以按需自动增长和缩小。切片是数组的引用,因此是引用类型,不支持直接比较,只能和nil比较。切片的动态增长是通过内建函数append()来实现的,这个函数可以快速且高效地增长切片,也可以通过切片再次切割,缩小每一个切片的大小。 切片不存值,底层数组存值 切片指向一个底层数组 底层数组是占用一块连续的内存空间 创建数组切片 创建两个类型分别为 int 型和 string 型的切片,并初始化 func main(){ var slice1 []int var slice2 []string fmt.Println(slice1,slice2) fmt.Println(slice1 == nil) //true,没有开辟内存空间 fmt.Println(...阅读全文

博文 2020-06-03 15:32:39 sunlingbot