Content deleted Content added
m Reverted edit by 2A02:FF0:254:424C:E53A:E385:A77:AE07 (talk) to last version by Kulukimaki |
added archive-url, added metadata, replaced outdated urls |
||
(17 intermediate revisions by 17 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">{{
| 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=
| 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++
There are two major implementations:
Line 73:
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.24 release<ref>{{Cite web|title=Go 1.24 Release Notes|url=https://go.dev/doc/go1.24|website=The Go Programming Language}}</ref> have maintained this promise.
Go uses a <code>go1.[major].[patch]</code> versioning 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>go1.24.0</code> the <code>24</code> is the major version.
==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===
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>
===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
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
====Lack of data race safety====
|