Global stocks sank Wednesday after US President Donald Trump said he was not satisfied with talks that are aimed at averting a trade war with China. Equities were also dented by poor eurozone economic data, and as Trump cast doubt on a planned summit with North Korean leader Kim Jong Un. “Trump (is) continuing to drive uncertainty over global trade,” said analyst Joshua Mahony at trading firm IG. “European markets are following their Asian counterparts lower, as a pessimistic tone from Trump is compounded by downbeat economic data,” he added. Markets had surged Monday after US Treasury Secretary Steven Mnuchin and Chinese Vice Premier Liu He said they had agreed to pull back from imposing threatened tariffs on billions of dollars of goods, and continue talks on a variety of trade issues. However, Trump has declared that he was “not satisfied” with the status of the talks, fuelling worries that the world’s top two economies could still slug out an economically pain
Go (often referred to as Golang) is a programming language created at Google in 2009 by Robert Griesemer, Rob Pike, and Ken Thompson.[11] Go is a statically typed language in the tradition of C, with memory safety, garbage collection, structural typing, and CSP-style concurrent programming features added. The compiler and other tools originally developed by Google are all free and open source.
Go originated as an experiment by Google engineers Robert Griesemer, Rob Pike, and Ken Thompson to design a new programming language that would resolve common criticisms of other languages while maintaining their positive characteristics. The developers envisioned the new language as:
- Statically typed and scalable to large systems (like Java or C++)
- Productive and readable, without excessive boilerplate(like dynamic languages such as Ruby or Python)
- Not requiring integrated development environments, but supporting them well
- Supporting networking and multiprocessing.
Go is recognizably in the tradition of C, but makes many changes to improve brevity, simplicity, and safety. Go consists of:
- A syntax and environment adopting patterns more common in dynamic languages:
- Optional concise variable declaration and initialization through type inference (
x := 0
notint x = 0;
orvar x = 0;
). - Fast compilation times.
- Remote package management (
go get
) and online package documentation.
- Optional concise variable declaration and initialization through type inference (
- Distinctive approaches to particular problems:
- Built-in concurrency primitives: light-weight processes (goroutines), channels, and the
select
statement. - An interface system in place of virtual inheritance, and type embedding instead of non-virtual inheritance.
Syntax
Go's syntax includes changes from C aimed at keeping code concise and readable. A combined declaration/initialization operator was introduced that allows the programmer to writei := 3
ors := "Hello, world!"
, without specifying the types of variables. This contrasts with C'sint i = 3;
andconst char *s = "Hello, world!";
. Semicolons still terminate statements, but are implicit when the end of a line occurs. Functions may return multiple values, and returning aresult, err
pair is the conventional way a function indicates an error to its caller in Go.[a] Go adds literal syntaxes for initializing struct parameters by name, and for initializing maps and slices. As an alternative to C's three-statementfor
loop, Go'srange
expressions allow concise iteration over arrays, slices, strings, maps, and channels.
Go has a number of built-in types, including numeric ones (byte, int64, float32, etc.), booleans, and character strings (string). Strings are immutable; built-in operators and keywords (rather than functions) provide concatenation, comparison, and UTF-8 encoding and decoding.[33] Record types can be defined with the struct keyword.[citation needed]
For each type T and each non-negative integer constant n, there is an array type denoted [n]T; arrays of differing lengths are thus of different types. Dynamic arrays are available as "slices", denoted []T for some type T. These have a length and a capacity specifying when new memory needs to be allocated to expand the array. Several slices may share their underlying memory.
type ipv4addr uint32
With this type definition, ipv4addr(x) interprets the uint32 value x as an IP address. Simply assigning x to a variable of type ipv4addr is a type error.
Constant expressions may be either typed or "untyped"; they are given a type when assigned to a typed variable if the value they represent passes a compile-time check.
Function types are indicated by the func keyword; they take zero or more parameters and return zero or more values, all of which are typed. The parameter and return values determine a function type; thus, func(string, int32) (int, error) is the type of functions that take a string and a 32-bit signed integer, and return a signed integer (of default width) and a value of the built-in interface type error.
Any named type has a method set associated with it. The IP address example above can be extended with a method for checking if its value is a known standard.
// ZeroBroadcast reports whether addr is 255.255.255.255.
func (addr ipv4addr) ZeroBroadcast() bool {
return addr == 0xFFFFFFFF
import "math"
type Shape interface {
Area() float64
}
type Square struct { // Note: no "implements" declaration
side float64
}
func (sq Square) Area() float64 { return sq.side * sq.side }
type Circle struct { // No "implements" declaration here either
radius float64
}
func (c Circle) Area() float64 { return math.Pi * math.Pow(c.radius, 2) }
Both Square and Circle are implicitly a Shape and can be assigned to a Shape-typed variable.[4In formal language, Go's interface system provides structural rather than nominal typing. Interfaces can embed other interfaces with the effect of creating a combined interface that is satisfied by exactly the types that implement the embedded interface and any methods that the newly defined interface adds.
Hello world
Here is a Hello world program in Go:
package main
import "fmt"
func main() {
fmt.Println("Hello, World")
}
Concurrency example
The following simple program demonstrates Go's concurrency features to implement an asynchronous program. It launches two "goroutines" (lightweight threads): one waits for the user to type some text, while the other implements a timeout. The select statement waits for either of these goroutines to send a message to the main routine, and acts on the first message to arrive (example adapted from David Chisnall book).
package main
import (
"fmt"
"time"
)
func readword(ch chan string) {
fmt.Println("Type a word, then hit Enter.")
var word string
fmt.Scanf("%s", &word)
ch <- word
}
func timeout(t chan bool) {
time.Sleep(5 * time.Second)
t <- true
}
func main() {
t := make(chan bool)
go timeout(t)
ch := make(chan string)
go readword(ch)
select {
case word := <-ch:
fmt.Println("Received", word)
case <-t:
fmt.Println("Timeout.")
}
Comments
Post a Comment