Go (programming language): Difference between revisions

Content deleted Content added
added archive-url, added metadata, replaced outdated urls
 
(31 intermediate revisions by 28 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 5:
{{Infobox programming language
| name = Go
| logo = [[File:Go Logo Blue.svg|frameless|220x80px]]
| logo size = 128px230px
| 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>
| logo caption =
| 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 16 ⟶ 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/}}
| dialects =
| influenced_by = {{#statements:influenced by}}
| 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 20092007 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 47:
Go was publicly announced in November 2009,<ref>{{Cite web |url=https://opensource.googleblog.com/2009/11/hey-ho-lets-go.html |title=Hey! Ho! Let's Go! |last1=Griesemer |first1=Robert |last2=Pike |first2=Rob |website=Google Open Source |access-date=May 17, 2018 |last3=Thompson |first3=Ken |last4=Taylor |first4=Ian |last5=Cox |first5=Russ |last6=Kim |first6=Jini |last7=Langley |first7=Adam}}</ref> and version 1.0 was released in March 2012.<ref>{{Cite news |last=Shankland |first=Stephen |url=https://www.cnet.com/news/googles-go-language-turns-one-wins-a-spot-at-youtube/ |title=Google's Go language turns one, wins a spot at YouTube: The lower-level programming language has matured enough to sport the 1.0 version number. And it's being used for real work at Google. |date=March 30, 2012 |access-date=August 6, 2017 |publisher=CBS Interactive Inc |department=News |quote=Google has released version 1 of its Go programming language, an ambitious attempt to improve upon giants of the lower-level programming world such as C and C++. |website=CNet}}</ref><ref>{{Cite web|url=https://golang.org/doc/devel/release.html|title=Release History |website=The Go Programming Language}}</ref> Go is widely used in production at Google<ref name="faq">{{Cite news |url=https://golang.org/doc/faq#internal_usage |title=Go FAQ: Is Google using Go internally? |access-date=March 9, 2013}}</ref> and in many other organizations and open-source projects.
 
In retrospect the Go authors judged Go to be successful due largely due to the overall engineering work around the language, including the runtime support for the language's concurrency feature.
 
{{Quotation|Although the design of most languages concentrates on innovations in syntax, semantics, or typing, Go is focused on the software development process itself. ... The principal unusual property of the language itself—concurrency—addressed problems that arose with the proliferation of multicore CPUs in the 2010s. But more significant was the early work that established fundamentals for packaging, dependencies, build, test, deployment, and other workaday tasks of the software development world, aspects
Line 71:
 
===Versioning===
Go 1 guarantees compatibility<ref>{{Cite web|url=https://golang.org/doc/go1compat|title=Go 1 and the Future of Go Programs |website=The Go Programming Language}}</ref> for the language specification and major parts of the standard library. All versions up through the current Go 1.2324 release<ref>{{Cite web|title=Go 1.2324 Release Notes|url=https://go.dev/doc/go1.2324|website=The Go Programming Language}}</ref> have maintained this promise.
 
Go doesuses not followa <code>go1.[major].[SemVerpatch]];</code> ratherversioning format, such as <code>go1.24.0</code> and each major Go release is supported until there are two newer major releases. Unlike most software, Go calls the second number in a version the major, i.e., in <code>1go1.x24.0</code> the <code>x24</code> is the major version. <ref>{{Cite web|url=https://golang.org/doc/devel/release.html#policy|title=Release History|website=The Go Programming Language}}</ref> This is because Go plans to never reach 2.0, givenprioritizing thatbackwards compatibility isover one of language's majorpotential sellingbreaking pointschanges.<ref>{{Cite web|url=https://go.dev/blog/compat|title=Backward Compatibility, Go 1.21, and Go 2|website=The Go Programming Language}}</ref>
 
==Design==
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===
Go's syntax includes changes from [[C (programming language)|C]] aimed at keeping code concise and readable. A combined declaration/initialization operator was introduced that allows the programmer to write <syntaxhighlight lang="Go" inline>i := 3</syntaxhighlight> or <syntaxhighlight lang="Go" inline>s := "Hello, world!"</syntaxhighlight>, [[type inference|without specifying the types]] of variables used. This contrasts with C's <syntaxhighlight lang="C" inline>int i = 3;</syntaxhighlight> and <syntaxhighlight lang="C" inline>const char *s = "Hello, world!";</syntaxhighlight>. Go also removes the requirement to use parentheses in if statement conditions.
 
Semicolons still terminate statements;{{efn|But "To allow complex statements to occupy a single line, a semicolon may be omitted before a closing ) or }".<ref>{{Cite web |url=https://golang.org/ref/spec#Semicolons |title=Go Programming Language Specification, §Semicolons |website=The Go Programming Language}}</ref>}} but are implicit when the end of a line occurs.{{efn|"if the newline comes after a token that could end a statement, [the lexer will] insert a semicolon".<ref>{{Cite web |url=https://golang.org/doc/effective_go.html#semicolons |title=Effective Go, §Semicolons |website=The Go Programming Language}}</ref>}}
 
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.
 
====Suitability for parallel programming====
Although Go's concurrency features are not aimed primarily at [[parallel computing|parallel processing]],{{r|concurrency-is-not}} they can be used to program [[shared -memory architecture|shared-memory]] [[multiprocessing|multi-processor]] machines. Various studies have been done into the effectiveness of this approach.<ref>{{cite conference |last=Tang |first=Peiyi |title=Multi-core parallel programming in Go |conference=Proc. First International Conference on Advanced Computing and Communications |year=2010 |url=http://www.ualr.edu/pxtang/papers/acc10.pdf |access-date=May 14, 2015 |archive-date=September 9, 2016 |archive-url=https://web.archive.org/web/20160909032631/http://www.ualr.edu/pxtang/papers/acc10.pdf |url-status=dead }}</ref> One of these studies compared the size (in [[lines of code]]) and speed of programs written by a seasoned programmer not familiar with the language and corrections to these programs by a Go expert (from Google's development team), doing the same for [[Chapel (programming language)|Chapel]], [[Cilk]] and [[Intel Threading Building Blocks|Intel TBB]]. The study found that the non-expert tended to write [[fork–join model|divide-and-conquer]] algorithms with one {{mono|go}} statement per recursion, while the expert wrote distribute-work-synchronize programs using one goroutine per processor core. The expert's programs were usually faster, but also longer.<ref>{{cite conference |last1=Nanz |first1=Sebastian |first2=Scott |last2=West |first3=Kaue |last3=Soares Da Silveira |title=Examining the expert gap in parallel programming |conference=Euro-Par 2013 |url=http://se.inf.ethz.ch/people/west/expert-gap-europar-2013.pdf |citeseerx=10.1.1.368.6137}}</ref>
 
====Lack of data race safety====