mirror of
https://github.com/jbranchaud/til
synced 2026-01-16 21:48:02 +00:00
Compare commits
4 Commits
5ce53a2510
...
98cf563e8c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
98cf563e8c | ||
|
|
9c0c9222f9 | ||
|
|
5615da920f | ||
|
|
c60c63f554 |
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1549 TILs and counting..._
|
||||
_1550 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -191,7 +191,7 @@ _1549 TILs and counting..._
|
||||
- [Aliasing An Ansible Host](devops/aliasing-an-ansible-host.md)
|
||||
- [Allow Cross-Origin Requests To Include Cookies](devops/allow-cross-origin-requests-to-include-cookies.md)
|
||||
- [Allow HTTPS Through Your UFW Firewall](devops/allow-https-through-your-ufw-firewall.md)
|
||||
- [Check For Cached Site Assocation File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
|
||||
- [Check For Cached Site Association File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
|
||||
- [Check The Status of All Services](devops/check-the-status-of-all-services.md)
|
||||
- [Check The Syntax Of nginx Files](devops/check-the-syntax-of-nginx-files.md)
|
||||
- [Connect To An RDS PostgreSQL Database](devops/connect-to-an-rds-postgresql-database.md)
|
||||
@@ -406,6 +406,7 @@ _1549 TILs and counting..._
|
||||
- [Build For A Specific OS And Architecture](go/build-for-a-specific-os-and-architecture.md)
|
||||
- [Check If Cobra Flag Was Set](go/check-if-cobra-flag-was-set.md)
|
||||
- [Combine Two Slices](go/combine-two-slices.md)
|
||||
- [Create A Slice From An Array](go/create-a-slice-from-an-array.md)
|
||||
- [Detect If Stdin Comes From A Redirect](go/detect-if-stdin-comes-from-a-redirect.md)
|
||||
- [Deterministically Seed A Random Number Generator](go/deterministically-seed-a-random-number-generator.md)
|
||||
- [Do Something N Times](go/do-something-n-times.md)
|
||||
@@ -749,7 +750,7 @@ _1549 TILs and counting..._
|
||||
- [Check If Clusters Are Upgrade Compatible](postgres/check-if-clusters-are-upgrade-compatible.md)
|
||||
- [Check If The Local Server Is Running](postgres/check-if-the-local-server-is-running.md)
|
||||
- [Check If User Role Exists For Database](postgres/check-if-user-role-exists-for-database.md)
|
||||
- [Check Table For Any Oprhaned Records](postgres/check-table-for-any-orphaned-records.md)
|
||||
- [Check Table For Any Orphaned Records](postgres/check-table-for-any-orphaned-records.md)
|
||||
- [Checking Inequality](postgres/checking-inequality.md)
|
||||
- [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md)
|
||||
- [Clear The Screen In psql](postgres/clear-the-screen-in-psql.md)
|
||||
|
||||
44
go/create-a-slice-from-an-array.md
Normal file
44
go/create-a-slice-from-an-array.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# Create A Slice From An Array
|
||||
|
||||
Slices in Go are a flexible abstraction over arrays. We can create a slice from
|
||||
an array with the `[n:m]` _slicing_ syntax. We specify the left and right
|
||||
(exclusive) bounds of the array that we want to create the slice relative to.
|
||||
|
||||
We can exclude the lower bound which translates to the `0` index of the array.
|
||||
We can exclude the left bound which translates to the end of the array. We can
|
||||
even exclude both ends of the _slicing_ syntax which means creating a slice of
|
||||
the entire array.
|
||||
|
||||
Here is an example of each of those:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
arr := [...]string{
|
||||
"taco",
|
||||
"burrito",
|
||||
"torta",
|
||||
"enchilada",
|
||||
"quesadilla",
|
||||
"pozole",
|
||||
}
|
||||
|
||||
firstTwo := arr[:2]
|
||||
lastTwo := arr[len(arr)-2:]
|
||||
all := arr[:]
|
||||
|
||||
fmt.Println("First two:", firstTwo)
|
||||
// First two: [taco burrito]
|
||||
|
||||
fmt.Println("Last two:", lastTwo)
|
||||
// Last two: [quesadilla pozole]
|
||||
|
||||
fmt.Println("All:", all)
|
||||
// All: [taco burrito torta enchilada quesadilla pozole
|
||||
}
|
||||
```
|
||||
|
||||
[source](https://go.dev/blog/slices-intro#slices)
|
||||
Reference in New Issue
Block a user