Go (programming language): Difference between revisions

Content deleted Content added
Avoid redirect.
added archive-url, added metadata, replaced outdated urls
 
(14 intermediate revisions by 14 users not shown)
Line 1:
{{short description|Programming language}}
{{for|the 2003 agent-based programming language|Go! (programming language)}}
{{Use American English|date=August 2022}}
Line 7:
| logo = Go Logo Blue.svg
| logo size = 230px
| paradigm = [[Multi-paradigm programming language|Multi-paradigm]]: [[concurrent programming|concurrent]], [[imperative programming|imperative]], [[functional programming|functional]],<ref>{{Cite web |url=https://go.dev/doc/codewalk/functions/ |title=Codewalk: First-Class Functions in Go |quote=Go supports first class functions, higher-order functions, user-defined function types, function literals, closures, and multiple return values. This rich feature set supports a functional programming style in a strongly typed language.}}</ref> [[object-oriented programming|object-oriented]]<ref>{{Cite web |url=https://golang.org/doc/faq#Is_Go_an_object-oriented_language |title=Is Go an object-oriented language? |access-date=April 13, 2019 |quote=Although Go has types and methods and allows an object-oriented style of programming, there is no type hierarchy.}}</ref><ref>{{Cite web |url=https://talks.golang.org/2012/chat.slide#5 |title=Go: code that grows with grace |access-date=June 24, 2018 |quote=Go is Object Oriented, but not in the usual way.}}</ref>
| year = {{start date and age|2009|11|10}}
| designer = [[Robert Griesemer]]<br />[[Rob Pike]]<br />[[Ken Thompson]]<ref name="langfaq" />
Line 15:
| latest_test_version =
| latest_test_date = <!--{{start date and age|2016|08|08}}<ref name="preview_page">{{Cite web |url=https://golang.org/dl/ |title=Release History |website=The Go Programming Language |access-date=August 8, 2016}}</ref>-->
| typing = [[type inference|Inferred]], [[static typing|static]], [[strong typing|strong]],<ref name="go-introduction">{{citeCite web |title=The Go Programming Language Specification |url=https://go.dev/ref/spec#Introduction |url-status=live title|archive-url=Thehttps://web.archive.org/web/20250813094237/https://go.dev/ref/spec#Introduction Go|archive-date=August Programming13, Language2025 Specification - the|work=The Go Programming Language }}</ref> [[structural typing|structural]],<ref name="structural_typing">{{Cite web |url=https://golang.org/doc/faq#implements_interface |title=Why doesn't Go have "implements" declarations? |websiteurl=Thehttps://go.dev/doc/faq#implements_interface Go|url-status=live Programming|archive-url=https://web.archive.org/web/20250728222723/https://go.dev/doc/faq#implements_interface Language|archive-date=July 28, 2025 |access-date=October 1, 2015 |website=The Go Programming Language }}</ref><ref name="pike-20141222">{{Cite webtweet |url=https://twitter.com/rob_pike/status/546973312543227904 |titlefirst=Rob Pike on Twitter |last=Pike |firstnumber=Rob546973312543227904 |dateuser=December 22, 2014rob_pike |access-date=March 13, 2016 |quotetitle=Go has structural typing, not duck typing. Full interface satisfaction is checked and required. |urlauthor-statuslink=deadRob Pike |date=December 22, 2014 |access-date=March 13, 2016 |archive-url=https://web.archive.org/web/20220407025913/https://twitter.com/rob_pike/status/546973312543227904 |archive-date=April 7, 2022 |url-04-07status=dead }}</ref> [[nominal typing|nominal]]
| memory management = [[Garbage collection (computer science)|Garbage collection]]
| implementations = gc, gofrontend, [[Elements_Toolchain|gold]]
| programming language = Go, [[Assembly language]] (gc); [[C++]] (gofrontend)
| website = {{url|https://go.dev/}}
Line 23:
| influenced = [[Crystal (programming language)|Crystal]], [[V (programming language)|V]]
| operating_system = [[DragonFly BSD]], [[FreeBSD]], [[Linux]], [[macOS]], [[NetBSD]], [[OpenBSD]],<ref name="openbsd">{{Cite web |url=http://ports.su/lang/go |title=lang/go: go-1.4 |date=December 23, 2014 |website=OpenBSD ports |access-date=January 19, 2015}}</ref> [[Plan 9 from Bell Labs|Plan 9]],<ref>{{Cite web |url=http://go-lang.cat-v.org/os-ports |title=Go Porting Efforts |date=January 12, 2010 |website=Go Language Resources |publisher=cat-v |access-date=January 18, 2010}}</ref> [[Solaris (operating system)|Solaris]], [[Windows]]
| license = [[3-clause BSD]]<ref name="license">{{Cite web |title=Text file LICENSE |url=httphttps://golanggo.orgdev/LICENSE |titleurl-status=Text filelive |archive-url=https://web.archive.org/web/20250716050530/https://go.dev/LICENSE |websitearchive-date=TheJuly Go16, Programming Language2025 |access-date=October 5, 2012 |website=The Go Programming Language }}</ref> + [[software patents|patent]] grant<ref name="gopatens">{{Cite web |title=Additional IP Rights Grant |url=httphttps://golanggo.orgdev/PATENTS |titleurl-status=Additionallive IP Rights Grant|archive-url=https://web.archive.org/web/20250330185005/https://go.dev/PATENTS |websitearchive-date=TheMarch Go30, Programming Language2025 |access-date=October 5, 2012 |website=The Go Programming Language }}</ref>
| file_ext = .go
}}
 
'''Go''' is a [[high-level programming language|high-level]] [[general purpose programming language]] that is [[static typing|statically typed]] and [[compiled language|compiled]]. It is known for the simplicity of its syntax and the efficiency of development that it enables by the inclusion of a large standard library supplying many needs for common projects.<ref>{{Cite web |title=Go Introduction |url=https://www.w3schools.com/go/go_introduction.php |access-date=2024-11-23 |website=www.w3schools.com |language=en-US}}</ref> It was designed at [[Google]]<ref name="techcrunch">{{Cite news |last=Kincaid |first=Jason |date=November 10, 2009 |title=Google's Go: A New Programming Language That's Python Meets C++ |language=en-US |work=TechCrunch |url=https://techcrunch.com/2009/11/10/google-go-language/ |archive-url= |archive-date= |access-date=January 18, 2010 |work=[[TechCrunch]] }}</ref> in 2007 by [[Robert Griesemer]], [[Rob Pike]], and [[Ken Thompson]], and publicly announced in November of 2009.<ref name="langfaq">{{Cite web |date=January 16, 2010 |title=Language Design FAQ |url=httphttps://golanggo.orgdev/doc/go_faqfaq |url-status=live |archive-url=https://web.htmlarchive.org/web/20250728222723/https://go.dev/doc/faq |archive-date=July 28, 2025 |access-date=February 27, 2010 |website=The Go Programming Language |language=en-US}}</ref> It is [[syntax (programming languages)|syntactically]] similar to [[C (programming language)|C]], but also has [[memory safety]], [[garbage collection (computer science)|garbage collection]], [[structural type system|structural typing]],<ref name="structural_typing" /> and [[communicating sequential processes|CSP]]-style [[concurrency (computer science)|concurrency]].<ref name="boldly">{{Cite webnews |urllast=https://www.theregister.co.uk/Metz |first=Cade |date=May 5, 2011/05/05/google_go/ |title=Google Go boldly goes where no code has gone before |lasturl=Metzhttps://www.theregister.com/2011/05/05/google_go/ |firsturl-status=Cadelive |archive-url=https://web.archive.org/web/20250728222723/https://go.dev/doc/faq |archive-date=MayJuly 528, 20112025 |websitework=[[The Register]] }}</ref> It is often referred to as '''Golang''' to avoid ambiguity and because of its former ___domain name, <code>golang.org</code>, but its proper name is Go.<ref>{{Cite web |url=https://go.dev/doc/faq#go_or_golang |title=Is the language called Go or Golang? |access-date=March 16, 2022 |quote=The language is called Go.}}</ref>
 
There are two major implementations:
Line 87:
** A toolchain that, by default, produces [[static library|statically linked]] native binaries without external Go dependencies
* A desire to keep the language specification simple enough to hold in a programmer's head,<ref>{{cite web|last=Pike|first=Rob|url=http://5by5.tv/changelog/100|title=The Changelog|type=Podcast|access-date=October 7, 2013|archive-date=October 20, 2013|archive-url=https://web.archive.org/web/20131020101046/http://5by5.tv/changelog/100|url-status=dead}}</ref> in part by [[#Omissions|omitting features that are common in similar languages]].
* 25 reserved words
 
===Syntax===
Line 94 ⟶ 95:
 
Methods may return multiple values, and returning a <syntaxhighlight lang="Go" inline="">result, err</syntaxhighlight> pair is the conventional way a method indicates an error to its caller in Go.{{efn|Usually, exactly one of the result and error values has a value other than the type's zero value; sometimes both do, as when a read or write can only be partially completed, and sometimes neither, as when a read returns 0 bytes. See [[Semipredicate problem#Multivalued return|Semipredicate problem: Multivalued return]].}} Go adds literal syntaxes for initializing struct parameters by name and for initializing [[Associative array|maps]] and [[Array slicing|slices]]. As an alternative to C's three-statement <code>for</code> loop, Go's <code>range</code> expressions allow concise iteration over arrays, slices, strings, maps, and channels.<ref>{{Cite web|url=https://golang.org/ref/spec#For_statements|title=The Go Programming Language Specification |website=The Go Programming Language}}</ref>
 
<code>fmt.Println("Hello World!")</code> is a statement.
 
In Go, statements are separated by ending a line (hitting the Enter key) or by a semicolon "<code>;</code>".
 
Hitting the Enter key adds "<code>;</code>" to the end of the line implicitly (does not show up in the source code).
 
The left curly bracket <code>{</code> cannot come at the start of a line.<ref>{{Cite web |title=Go Syntax |url=https://www.w3schools.com/go/go_syntax.php |access-date=2024-11-23 |website=www.w3schools.com |language=en-US}}</ref>
 
===Types===
Line 230 ⟶ 223:
==={{anchor|Concurrency}} Concurrency: goroutines and channels===
[[File:DotGo 2015 - Matt Aimonetti - Applied concurrency in Go.webm|thumb|DotGo 2015 - Matt Aimonetti - Applied concurrency in Go]]
The Go language has built-in facilities, as well as library support, for writing [[concurrent programming|concurrent programs]]. The runtime is [[asynchronous I/O|asynchronous]]: program execution that performs, for example, a network read will be suspended until data is available to process, allowing other parts of the program to perform other work. This is built into the runtime and does not require any changes in program code. The go runtime also automatically schedules concurrent operations (goroutines) across multiple CPUs-; this can achieve parallelism for a properly written program.<ref name="concurrency-is-not">{{cite web|last=Pike |first=Rob |url=http://vimeo.com/49718712 |title=Concurrency is not Parallelism|date=September 18, 2012 }}</ref>
 
The primary concurrency construct is the ''goroutine'', a type of [[green thread]].<ref name=":0">{{Cite book |last1=Donovan |first1=Alan A. A. |title=The Go programming language |last2=Kernighan |first2=Brian W. |date=2016 |publisher=Addison-Wesley |isbn=978-0-13-419044-0 |series=Addison-Wesley professional computing series |___location=New York, Munich |pages=}}</ref>{{Rp|pages=280-281}} A function call prefixed with the <code>go</code> keyword starts a function in a new goroutine. The language specification does not specify how goroutines should be implemented, but current implementations multiplex a Go process's goroutines onto a smaller set of [[thread (computer science)|operating-system threads]], similar to the scheduling performed in [[Erlang (programming language)|Erlang]] and [[Haskell (programming language)|Haskell's GHC runtime implementation]].{{r|phrasebook}}{{rp|10}}
Line 240 ⟶ 233:
The existence of channels does not by itself set Go apart from [[actor model]]-style concurrent languages like Erlang, where messages are addressed directly to actors (corresponding to goroutines). In the actor model, channels are themselves actors, therefore addressing a channel just means to address an actor. The actor style can be simulated in Go by maintaining a one-to-one correspondence between goroutines and channels, but the language allows multiple goroutines to share a channel or a single goroutine to send and receive on multiple channels.{{r|phrasebook}}{{rp|147}}
 
From these tools one can build concurrent constructs like [[Thread pool|worker pools]], pipelines (in which, say, a file is decompressed and parsed as it downloads), background calls with timeout, "fan-out" parallel calls to a set of services, and others.<ref>{{Cite web |url=http://talks.golang.org/2012/concurrency.slide |title=Go Concurrency Patterns |website=The Go Programming Language}}</ref> Channels have also found uses further from the usual notion of interprocess communication, like serving as a concurrency-safe list of recycled buffers,<ref>{{cite web|last=Graham-Cumming|first=John|url=http://blog.cloudflare.com/recycling-memory-buffers-in-go|title=Recycling Memory Buffers in Go|work=The Cloudflare Blog |date=August 24, 2013}}</ref> implementing [[coroutine]]s (which helped inspire the name ''goroutine''),<ref>{{Cite web|url=http://golang.org/doc/play/tree.go|title=tree.go}}</ref> and implementing [[iterator]]s.<ref>{{cite web|last=Cheslack-Postava |first=Ewen |url=http://ewencp.org/blog/golang-iterators/ |title=Iterators in Go}}</ref>
 
Concurrency-related structural conventions of Go ([[channel (programming)|channel]]s and alternative channel inputs) are derived from [[C. A. R. Hoare|Tony Hoare's]] [[communicating sequential processes]] model. Unlike previous concurrent programming languages such as [[Occam (programming language)|Occam]] or [[Limbo (programming language)|Limbo]] (a language on which Go co-designer Rob Pike worked),<ref>{{cite web|last=Kernighan|first=Brian W.|url=http://www.vitanuova.com/inferno/papers/descent.html|title=A Descent Into Limbo}}</ref> Go does not provide any built-in notion of safe or verifiable concurrency.<ref name="memmodel">{{cite web |url=http://golang.org/doc/go_mem.html |title=The Go Memory Model |access-date=January 5, 2011}}</ref> While the communicating-processes model is favored in Go, it is not the only one: all goroutines in a program share a single address space. This means that mutable objects and pointers can be shared between goroutines; see {{slink||Lack of data race safety}}, below.