George  Koelpin

George Koelpin

1597468080

Understanding Slices in Go Programming

Since I started programming in Go the concept and use of slices has been confusing. This is something completely new to me. They look like an array, and feel like an array, but they are much more than an array. I am constantly reading how slices are used quite a bit by Go programmers and I think it is finally time for me to understand what slices are all about.

There is a great blog post written by Andrew Gerrand about slices:

http://blog.golang.org/go-slices-usage-and-internals

There is no reason to repeat everything Andrew has written so please read his post before continuing. Let’s just cover the internals of a slice.

Screen ShotScreen Shot

The picture above represents the internal structure of a slice. When you allocate a slice this data structure along with an underlying array is created. The value of your slice variable will be this data structure. When you pass a slice to a function, a copy of this data structure is created on the stack.

We can create a slice in two ways:

Here we use the keyword make,  passing the type of data we are storing, the initial length of the slice and the capacity of the underlying array.

mySlice := make([]string, 5, 8)
mySlice[0] = "Apple"
mySlice[1] = "Orange"
mySlice[2] = "Banana"
mySlice[3] = "Grape"
mySlice[4] = "Plum"

// You don’t need to include the capacity. Length and Capacity will be the same
mySlice := make([]string, 5)

You can also use a slice literal. In this case the length and capacity will be the same. Notice no value is provided inside the hard brackets []. If you add a value you will have an array. If you don’t add a value you will have a slice.

mySlice := []string{"Apple", "Orange", "Banana", "Grape", "Plum"}

You can’t extend the capacity of a slice once it is created. The only way to change the capacity is to create a new slice and perform a copy. Andrew provides a great sample function that shows an efficient way to check the remaining capacity for room and only if necessary, perform a copy.

The length of a slice identifies the number of elements of the underlying array we are using from the starting index position. The capacity identifies the number of elements we have available for use.

We can create a new slice from the original slice:

newSlice := mySlice[2:4]

Screen Shot

The value of the new slice’s pointer variable is associated with index positions 2 and 3 of the initial underlying array. As far as this new slice is concerned, we now have a underlying array of 3 elements and we are only using 2 of those 3 elements. This new slice has no knowledge of the first two elements from the initial underlying array and never will.

When performing a slice operation the first parameter specifies the starting index from the slices pointer variable position. In our case we said index 2 which is 3 elements inside the initial underlying array we are taking the slice from. The second parameter is the last index position plus one (+1). In our case we said index 4 which will include all indexes between index 2 (the starting position) and index 3 (the final position).

#go

What is GEEK

Buddha Community

Understanding Slices in Go Programming
Fannie  Zemlak

Fannie Zemlak

1599854400

What's new in the go 1.15

Go announced Go 1.15 version on 11 Aug 2020. Highlighted updates and features include Substantial improvements to the Go linker, Improved allocation for small objects at high core counts, X.509 CommonName deprecation, GOPROXY supports skipping proxies that return errors, New embedded tzdata package, Several Core Library improvements and more.

As Go promise for maintaining backward compatibility. After upgrading to the latest Go 1.15 version, almost all existing Golang applications or programs continue to compile and run as older Golang version.

#go #golang #go 1.15 #go features #go improvement #go package #go new features

Learning Go — Array, Slice, Map

We have already seen how to create and work with variables, pointers, and Constants. In this article, we are going to see the four important collection types built into the language.
We start with humble Arrays, from there we move on to Slice, an evolved version of Array. From Slice, we talk about the map a key-value pair collection. Let’s start with Arrays here.

#golang-tutorial #golang #go-programming #go-programming-language #programming

Zander  Herzog

Zander Herzog

1596793260

Secure HTTPS servers in Go

In this article, we are going to look at some of the basic APIs of the http package to create and initialize HTTPS servers in Go.

Image for post

(source: unsplash.com)

In the “Simple Hello World Server” lesson, we learned about net/http package, how to create routes and how [ServeMux](https://golang.org/pkg/net/http/#ServeMux) works. In the “Running multiple HTTP servers” lesson, we learned about [Server](https://golang.org/pkg/net/http/#Server) structure and how to run multiple HTTP servers concurrently.

In this lesson, we are going to create an HTTPS server using both Go’s standard server configuration and custom configuration (using [_Server_](https://golang.org/pkg/net/http/#Server) structure). But before this, we need to know what HTTPS really is?

HTTPS is a big topic of discussion in itself. Hence while writing this lesson, I published an article just on “How HTTPS works?”. I advise you to read this lesson first before continuing this article. In this article, I’ve also described the encryption paradigm and SSL certificates generation process.


If we recall the simplest HTTP server example from previous lessons, we only need http.``[ListenAndServe](https://golang.org/pkg/net/http/#ListenAndServe) function to start an HTTP server and http.``[HandleFunc](https://golang.org/pkg/net/http/#HandleFunc) to register a response handler for a particular endpoint.

Image for post

(https://play.golang.org/p/t3sOenOYAzS)

In the example above, when we run the command go run server.go , it will start an HTTP server on port 9000. By visiting http://localhost:9000 URL in a browser, you will be able to see a Hello World! message on the screen.

Image for post

(http://localhost:9000)

As we know, the nil argument to ListenAndServe() call invokes Go to use the [DefaultServeMux](https://golang.org/pkg/net/http/#DefaultServeMux) response multiplexer, which is the default instance of ServeMux structure provided globally by the Go. The HandleFunc() call adds a response handler for a specific route on the multiplexer instance.

The http.ListenAndServe() call uses the Go’s standard HTTP server configuration, however, in the previous lesson, how we can customize a server using [Server](https://golang.org/pkg/net/http/#Server) structure type.

To start an HTTPS server, all we need do is to call ServerAndListenTLS method with some configuration. Just like ServeAndListen method, this method is available on both the http package and the Server structure.

The http.``[ServeAndListenTLS](https://golang.org/pkg/net/http/#ListenAndServeTLS) method uses the Go’s standard server implementation, however, both [Server](https://golang.org/pkg/net/http/#Server) instance and Server.``[ServeAndListenTLS](https://golang.org/pkg/net/http/#Server.ListenAndServeTLS) method can be configured for our needs.

#go-programming-language #go #golang-tutorial #go-programming #golang

Zander  Herzog

Zander Herzog

1596734389

Go (Golang): Your Cheatsheet On Ways To Create New Maps And Slices

There are quite a few ways to create new maps and slices in Go. Which one is best? Or perhaps better asked, which one is best in your situation? Let’s take a look.

Slices

var varStyle []string

literalStyle := []string{}

newStyle := new([]string)

makeStyle := make([]string, 0)

var varStyle []string is the idiomatic way to declare an empty slice. The slice is actually nil, which means it will be null when marshalled to JSON and will succeed nil checks.

literalStyle := []string{} should probably only be used when the literal is going to start with values in it, as in literalStyle := []string{“cat”, “dog”, etc}. Otherwise prefer make()

newStyle := new([]string) returns a pointer to the slice. Same as ptrStyle := &[]string{}Only use if you want a pointer.

makeStyle := make([]string, 0) is the same as the literal style, but is preferred for idiomatic reasons when the slice doesn’t need non-zero starting values. Make() allows the slice to be initialized with a starting length and capacity, which can have good performance implications in some circumstances:

makeStyle := make([]string, len, cap)

#golang #go #make #map #slice #programming

Make, New & Literals Cheat Sheet - Slice and Map Initialization in Go

There are quite a few ways to create new maps and slices in Go. Which one is best? Or perhaps better asked, which one is best in your situation? Let’s take a look.

Slices

var varStyle []string

literalStyle := []string{}

newStyle := new([]string)

makeStyle := make([]string, 0)

var varStyle []string is the idiomatic way to declare an empty slice. The slice is actually nil, which means it will be null when marshalled to JSON and will succeed nil checks.

literalStyle := []string{} should probably only be used when the literal is going to start with values in it, as in literalStyle := []string{“cat”, “dog”, etc}. Otherwise prefer make()

newStyle := new([]string) returns a pointer to the slice. Same as ptrStyle := &[]string{}Only use if you want a pointer.

makeStyle := make([]string, 0) is the same as the literal style, but is preferred for idiomatic reasons when the slice doesn’t need non-zero starting values. Make() allows the slice to be initialized with a starting length and capacity, which can have good performance implications in some circumstances:

makeStyle := make([]string, len, cap)

#clean code #golang #programming #go #golang #make #map #slice