Explore all Math open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Math

KaTeX

v0.15.3

synapse

v1.57.1

gonum

v0.9.3

pywonderland

decimal

v1.3.1

Popular Libraries in Math

KaTeX

by KaTeX doticonjavascriptdoticon

star image 15047 doticonMIT

Fast math typesetting for the web.

mathjs

by josdejong doticonjavascriptdoticon

star image 12216 doticonApache-2.0

An extensive math library for JavaScript and Node.js

synapse

by matrix-org doticonpythondoticon

star image 9347 doticonApache-2.0

Synapse: Matrix homeserver written in Python 3/Twisted.

gonum

by gonum doticongodoticon

star image 5589 doticonBSD-3-Clause

Gonum is a set of numeric libraries for the Go programming language. It contains libraries for matrices, statistics, optimization, and more

bignumber.js

by MikeMcl doticonjavascriptdoticon

star image 5125 doticonMIT

A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic

awesome-math

by rossant doticonpythondoticon

star image 4556 doticon

A curated list of awesome mathematics resources

decimal.js

by MikeMcl doticonjavascriptdoticon

star image 4239 doticonMIT

An arbitrary-precision Decimal type for JavaScript

pywonderland

by neozhaoliang doticonpythondoticon

star image 3975 doticonMIT

A tour in the wonderland of math with python.

decimal

by shopspring doticongodoticon

star image 3919 doticonNOASSERTION

Arbitrary-precision fixed-point decimal numbers in go

Trending New libraries in Math

handcalcs

by connorferster doticoncssdoticon

star image 3266 doticonApache-2.0

Python library for converting Python calculations into rendered latex.

latexify_py

by google doticonpythondoticon

star image 1747 doticonApache-2.0

Generates LaTeX math description from Python functions.

notecalc3

by bbodi doticonrustdoticon

star image 962 doticonAGPL-3.0

NoteCalc is a handy calculator trying to bring the advantages of Soulver to the web.

MathInspector

by MathInspector doticonpythondoticon

star image 877 doticonGPL-3.0

A visual programing environment for scientific computing with python

Mathfs

by FreyaHolmer doticoncsharpdoticon

star image 728 doticonMIT

Expanded Math Functionality for Unity

matrix-rust-sdk

by matrix-org doticonrustdoticon

star image 468 doticonApache-2.0

Matrix Client-Server SDK for Rust

remix-utils

by sergiodxa doticontypescriptdoticon

star image 424 doticonMIT

A set of utility functions and types to use with Remix.run

SoulverCore

by soulverteam doticonswiftdoticon

star image 353 doticon

A powerful Swift framework for evaluating mathematical expressions

CalcuLaTeX

by mkhan45 doticonrustdoticon

star image 337 doticonMPL-2.0

A pretty printing calculator language with support for units. Makes calculations easier and more presentable with real time LaTeX output, along with support for units, variables, and mathematical functions.

Top Authors in Math

1

planetmath

27 Libraries

star icon130

2

matrix-org

17 Libraries

star icon10964

3

math-io

14 Libraries

star icon36

4

scijs

12 Libraries

star icon84

5

nschloe

11 Libraries

star icon1430

6

mikolalysenko

10 Libraries

star icon181

7

compute-io

10 Libraries

star icon44

8

matrix-io

9 Libraries

star icon134

9

BaseMax

9 Libraries

star icon52

10

rafl

7 Libraries

star icon17

1

27 Libraries

star icon130

2

17 Libraries

star icon10964

3

14 Libraries

star icon36

4

12 Libraries

star icon84

5

11 Libraries

star icon1430

6

10 Libraries

star icon181

7

10 Libraries

star icon44

8

9 Libraries

star icon134

9

9 Libraries

star icon52

10

7 Libraries

star icon17

Trending Kits in Math

Here are some of the famous NodeJs Math Libraries. Some use cases of NodeJs Math Libraries include Scientific Computing, Data Analysis, Statistical Analysis, and Machine Learning.

 

Node.js math libraries refer to libraries that provide mathematical functions and utilities for Node.js applications. These libraries can perform calculations, create graphs, and more. Some of these libraries include mathjs, math-expression-evaluator, math-expression-parser, and mathjs-expression-parser.  


Let us look at the libraries in detail below. 

Math.js 

  • Library that covers a wide range of topics from basic arithmetic to advanced calculus.  
  • Provides intuitive syntax for expressing mathematical expressions.  
  • Offers various functions for performing mathematical operations, including linear algebra, calculus, and statistical operations.

JStat 

  • Faster and easier to understand and use than other math libraries written in other languages. 
  • Well-documented and provides helpful examples. 
  • Open-source, developers can contribute and extend the library to their own needs.  

Numeral-js 

  • Optimized for modern JavaScript runtimes. 
  • Built on top of the popular math.js library, providing an easy transition from other math libraries.  
  • Supports multiple precision types, including BigInt, BigDecimal, and Number.  

stdlib 

  • More than just lines of code are needed to create a wide range of mathematical algorithms.   
  • Provides basic and advanced operations.  
  • Is designed to provide high-performance calculations, making it ideal for applications requiring fast results.   

fcal 

  • Offer optimized functions and vector operations.  
  • Offers comprehensive linear algebra support, including matrix operations and advanced numerical algorithms.  
  • Offers support for complex numbers and quaternions. 

clumsy 

  • Written in pure JavaScript and is not dependent on any external libraries.  
  • Allows the creation of custom-made mathematical equations that can be used in the code.  
  • Allows incorporation of numerical integration and optimization. 

KaTex 

  • Is faster than other nodejs math libraries, with its rendering time around 10ms. 
  • Reliable than other nodejs math libraries, as it is more robust and provides more consistent results. 
  • Better typography for mathematical expressions than other nodejs math libraries. 

prime-num 

  • Only the Node.js library offers a fast and simple prime number generation algorithm.  
  • Well-tested and reliable, and accurate   
  • A built-in caching system that allows for quick lookups of previously generated prime numbers. 

primeupto 

  • Has an extensive test suite that ensures that results are accurate.  
  • Functions for generating prime numbers up to a specific limit and detecting prime numbers.   
  • Supports both synchronous and asynchronous calculations. 

Math-PKG 

  • Written in pure JavaScript and designed to perform Mathematical operations efficiently.   
  • Powerful API for developers to create their own custom functions.  
  • Compatible with the latest version of Node.js and can be used with other popular frameworks. 

Here are some famous C++ Math Libraries. Some C++ Math Libraries' use cases include Numerical Analysis, Statistical Analysis, Machine Learning, Data Visualization, Image Processing, and Scientific Computing.


Cpp math libraries are libraries of mathematical functions written in C++ designed to aid in the development of applications that require complex mathematical operations. They provide a set of tools to allow developers to quickly and easily access math functions and calculations.


Let us look at some libraries in detail.

dlib

  • Contains a wide variety of optimized algorithms and data structures.
  • Provides tools for linear algebra, machine learning, and image processing.
  • Has a number of utilities for generating random numbers.

root

  • Provide multi-dimensional histograms and graphs.
  • Advanced statistical analysis and data fitting.
  • Highly modular and extensible, making it easy to add new features or extend existing ones.

eigen

  • Optimized for speed and memory-efficiency to provide the best possible performance.
  • Supports both dense and sparse matrices, as well as dynamic-sized matrices.
  • Provides an intuitive and expressive API that makes it easy to write code with.

blitz

  • Has an intuitive syntax that allows for easy coding and debugging.
  • Provides built-in support for parallelization and distributed computing.
  • Provides built-in support for parallelization and distributed computing.

blaze

  • Compatible with a wide range of compilers.
  • Supports both dense and sparse matrices, enabling efficient calculations with large datasets.
  • Able to perform complex calculations with a minimal amount of code.

armadillo

  • Provides a wide range of operations on matrices, vectors and scalars.
  • Includes support for matrix decompositions, Fourier transforms, advanced linear algebra operations and random number generation.
  • Written in expressive modern C++ code, taking advantage of C++11 features such as template metaprogramming

GiNaC

  • Has a sophisticated yet easy to use interface.
  • Features an extensive set of functions, such as integrals, series, and derivatives.
  • Allows users to extend their applications by adding custom functions.

MathGL

  • Supports the OpenGL and OpenCL graphics libraries.
  • Has a built-in library of special mathematical functions, such as Bessel, Legendre and Chebyshev functions.
  • Wide range of plotting capabilities, with over 40 different types of 2D and 3D plots.

Go Math Libraries offer various features, making working with mathematical computations and analysis easy. These libraries offer various mathematical functions which can be used for performing common calculations. These functions include logarithmic functions, trigonometric functions, and many more. 


The Go Math programming language is known for offering high performance, so the math libraries built with Go are often fast and efficient. It makes it ideal for larger datasets or when performing complex calculations. They also offer various data structures which can be used for storing and manipulating mathematical data like matrices, vectors, and different data types. It also offers tools for visualizing mathematical data like charts and graphs. These tools will help make complex mathematical concepts easy to understand. 


Here is the list of the top 6 Go Math Libraries handpicked to help developers perform mathematical computations and analysis easily and efficiently. 

gonum: 

  • Offers a package for performing linear algebra operations like vector operations, matrix operations, and solving linear systems of equations. 
  • Provides a package for optimization that includes constrained, unconstrained, and gradient-based optimization algorithms. 
  • Offers a package for working with graphs, like algorithms for shortest path finding, graph traversal, and many more.  

stats: 

  • Offers functions for calculating basic statistics like median, variance, standard deviation, mean, and mode. 
  • Offers functions for working with a probability distribution, like calculating probability density functions, generating random numbers from different distributions, and cumulative distribution functions. 
  • Offers functions for conducting hypothesis tests like chi-squared tests, ANOVA tests, and t-tests. 

gosl: 

  • Provides various numerical methods like numerical integration, optimization algorithms, and root finding. 
  • Provides a package for linear algebra operations like vector and matrix operations, eigenvalue and eigenvector calculations, linear systems solve, and more. 
  • Offers a package for solving differential equations, including partial differential equations and ordinary differential equations. 

mathgl: 

  • Provides various tools for creating 2D and 3D plots of mathematical data like line plots, scatter plots, contour plots, and more. 
  • Offers functions for visualizing vector fields, including vector fields and streamline plots. 
  • Offers a package for symbolic math operations like integration, simplification, and differentiation of mathematical equations.  

fixed: 

  • Offers functions for performing arithmetic operations like subtraction, addition, division, and multiplication. 
  • Provides functions for calculating trigonometric functions like cosine, sine, and tangent. 
  • Offers functions for calculating logarithmic and exponential functions like the base 2 and natural logarithm.  

gomatrix: 

  • Provides various matrix operations like matrix addition, subtraction, multiplication, division, and many more.  
  • Offers functions for linear algebra operations like matrix decomposition, eigenvector, and eigenvalue calculations, inversion, singular value decomposition, and many more.  
  • Offers functions for vector operations like cross and dot product and normalization.  

C# Math Libraries provide various functionalities and features which can be used for different mathematical calculations. C# Math libraries provide basic math functions like multiplication, division, addition, and subtraction. It offers trigonometric functions like cosine, sine, and tangent. 

 

C# Math libraries provide logarithm and exponential functions like natural logarithm, exponent, and logarithm base 10. It offers support for complex math and number functions. It offers statistical functions like median, variance, mean, mode, correlation coefficient, and standard deviation. It supports linear algebra operations like eigenvectors and eigenvalues, matrix multiplication, and determinants. It offers numerical analysis functions like differentiation, solving differential equations, and numerical integration.  


Here is the list of the top 7 C# Math Libraries handpicked for helping developers with mathematical functions.  

MathUtilities:  

  • Includes additional trigonometric functions like secant, cotangent, and cosecant.  
  • Include functions for performing linear algebra operations like solving equations and finding eigenvectors.  
  • Offers a range of additional mathematical utilities and functions which can be used in engineering, financial, and scientific applications.  

mathnet-numerics:  

  • Is an open source library that offers various mathematical tools and functions for numerical computing.  
  • Offers trigonometric functions like a tangent, inverse trigonometric functions, sine, and cosine.  
  • Offers a complete set of linear algebraic operations like determinants, Cholesky decomposition, QR decomposition, SVD decomposition, and eigenvalues and eigenvectors.  

Unity.Mathematics:  

  • Offers a set of math functions and datatypes designed to be high-performance and optimized for real-time applications.  
  • Offers various matrix datatypes and vectors like 2D, 3D, and 4D vectors and projections, transformations, and other matrix operations.  
  • Offers interpolation functions for smooth transitioning between values over time, like cubic, linear, and spline interpolation.  

Mathfs:  

  • Includes functions for working with quaternions, a mathematical concept used for representing rotations in 3D space.  
  • Includes functions for testing intersections between geometric shapes like lines, spheres, and points.  
  • Includes functions for creating and testing bounding volumes used in game development for optimizing collision detection and other calculations.  

MathSharp:  

  • Is an open source math library for C# which offers high-performance math functions for matrix and vector operations.  
  • Offers trigonometric functions like inverse trigonometric, sine, tangent, and cosine functions.  
  • Offers interpolation functions for smoothly transitioning between values over time, like cubic, spline, and linear interpolation.  

AngouriMath:  

  • Is an open source symbolic math library for C# that offers various functions for algebraic manipulation, calculus, and other mathematical operations.  
  • Offers an extensible and flexible system to represent symbolic expressions that allow complex mathematical simplifications and operations.  
  • Offers a comprehensive set of tools and functions for symbolic math operations, making it best for applications that need precise mathematical manipulations and calculations.  

LockstepEngine:  

  • Is an open source game engine for C# that offers a deterministic game loop designed for multiplayer games.  
  • Offers a deterministic game loop that ensures all players see the same game state simultaneously, regardless of their performance difference and local input.  
  • Provides a customizable input system that allows developers to handle diverse input types, like mouse, keyboard, and gamepad. 

SciPy is a free and open-source Python library used for scientific computing and technical computing. It is a collection of mathematical algorithms and convenience functions built on the NumPy extension of Python. It adds significant power to the interactive Python session by providing the user with high-level commands and classes for manipulating and visualizing data.

Code

In this solution, we use the lognorm function of the Scipy library

  1. Copy the code using the "Copy" button above, and paste it in a Python file in your IDE.
  2. Modify the values.
  3. Run the file, check the output in terminal.


I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.

Dependent Libraries

If you do not have Scipy required to run this code, you can install it by clicking on the above link and copying the pip Install command from the Scipy page in Kandi.

Environment Tested 

I tested this solution in the following versions. Be mindful of changes when working with other versions. 

  1. The solution is created in Python3.11. 
  2. The solution is tested on Scipy 1.10 version. 

Support

  1. For any support on kandi solution kits, please use the chat
  2. For further learning resources, visit the Open Weaver Community learning page.

Trending Discussions on Math

How can I merge two dataframes together with some conditional requirements?

Find occurrence count of the longest common Prefix/Suffix in a List of Strings?

Differences in floating point between JDK 8 and JDK 13

Using NativeCall to call the C fn `erf` gets more precise output than `erf` in C

Repeatedly removing the maximum average subarray

How do I calculate square root in Python?

JMH using java 17, no dead code elimination

Docker standard_init_linux.go:228: exec user process caused: no such file or directory

How to apply one signature test to multiple positionals

How to generate a Rank 5 matrix with entries Uniform?

QUESTION

How can I merge two dataframes together with some conditional requirements?

Asked 2022-Mar-22 at 14:33

I have two dataframes, df1 and df2. I would like to join the two with the following conditions:

  1. merge df1 and df2 on gender and Test
  2. TestDate in df1 need to be within Date1 and Date2 from df2
  3. all.x = TRUE (keep df1 records)

How can I handle the second part?

Enter image description here

1df1 <- structure(list(ID = c(1, 2, 3, 5, 4), Gender = c("F", "M", "M", 
2"F", "F"), TestDate = structure(c(17897, 17898, 18630, 18262, 
317900), class = "Date"), Test = c("Weight", "Weight", "ELA", 
4"ELA", "Math")), row.names = c(NA, -5L), class = c("tbl_df", 
5"tbl", "data.frame"))
6
7
8df2 <- structure(list(Test = c("Weight", "Weight", "ELA", "ELA", "ELA", 
9"ELA", "Math", "Math"), Gender = c("F", "M", "F", "M", "F", "M", 
10"F", "M"), Date1 = structure(c(17532, 17534, 17536, 17537, 18266, 
1118267, 17897, 17539), class = "Date"), Ave = c(97, 99, 85, 84, 
1283, 82, 88, 89), Date2 = structure(c(18993, 18995, 18266, 18267, 
1318997, 18998, 18999, 19000), class = "Date")), row.names = c(NA, 
14-8L), class = c("tbl_df", "tbl", "data.frame"))
15

ANSWER

Answered 2022-Mar-21 at 14:44

Does this work for you?

1df1 <- structure(list(ID = c(1, 2, 3, 5, 4), Gender = c("F", "M", "M", 
2"F", "F"), TestDate = structure(c(17897, 17898, 18630, 18262, 
317900), class = "Date"), Test = c("Weight", "Weight", "ELA", 
4"ELA", "Math")), row.names = c(NA, -5L), class = c("tbl_df", 
5"tbl", "data.frame"))
6
7
8df2 <- structure(list(Test = c("Weight", "Weight", "ELA", "ELA", "ELA", 
9"ELA", "Math", "Math"), Gender = c("F", "M", "F", "M", "F", "M", 
10"F", "M"), Date1 = structure(c(17532, 17534, 17536, 17537, 18266, 
1118267, 17897, 17539), class = "Date"), Ave = c(97, 99, 85, 84, 
1283, 82, 88, 89), Date2 = structure(c(18993, 18995, 18266, 18267, 
1318997, 18998, 18999, 19000), class = "Date")), row.names = c(NA, 
14-8L), class = c("tbl_df", "tbl", "data.frame"))
15library(dplyr)
16library(data.table)
17merge(x = df1, 
18      y = df2) %>% 
19  filter(TestDate %between% list(Date1, Date2))
20

Source https://stackoverflow.com/questions/71559360

QUESTION

Find occurrence count of the longest common Prefix/Suffix in a List of Strings?

Asked 2022-Mar-22 at 07:13

Given a list of Strings:

1ArrayList<String> strList = new ArrayList<String>();
2strList.add("Mary had a little lamb named Willy");
3strList.add("Mary had a little ham");
4strList.add("Old McDonald had a farm named Willy");
5strList.add("Willy had a little dog named ham");
6strList.add("(abc)");
7strList.add("(xyz)");
8strList.add("Visit Target Store");
9strList.add("Visit Walmart Store");
10

This should produce the output in the form of a HashMap<String, Integer> prefixMap and suffixMap:

PREFIX:

1ArrayList&lt;String&gt; strList = new ArrayList&lt;String&gt;();
2strList.add(&quot;Mary had a little lamb named Willy&quot;);
3strList.add(&quot;Mary had a little ham&quot;);
4strList.add(&quot;Old McDonald had a farm named Willy&quot;);
5strList.add(&quot;Willy had a little dog named ham&quot;);
6strList.add(&quot;(abc)&quot;);
7strList.add(&quot;(xyz)&quot;);
8strList.add(&quot;Visit Target Store&quot;);
9strList.add(&quot;Visit Walmart Store&quot;);
10Mary had a -&gt; 2
11Mary had a little -&gt; 2
12( -&gt; 2
13Visit -&gt; 2
14

SUFFIX:

1ArrayList&lt;String&gt; strList = new ArrayList&lt;String&gt;();
2strList.add(&quot;Mary had a little lamb named Willy&quot;);
3strList.add(&quot;Mary had a little ham&quot;);
4strList.add(&quot;Old McDonald had a farm named Willy&quot;);
5strList.add(&quot;Willy had a little dog named ham&quot;);
6strList.add(&quot;(abc)&quot;);
7strList.add(&quot;(xyz)&quot;);
8strList.add(&quot;Visit Target Store&quot;);
9strList.add(&quot;Visit Walmart Store&quot;);
10Mary had a -&gt; 2
11Mary had a little -&gt; 2
12( -&gt; 2
13Visit -&gt; 2
14named Willy -&gt; 2
15ham -&gt; 2
16) -&gt; 2
17Store -&gt; 2
18

So far I'm able to generate a prefix that is present in all items in list using the following code:

1ArrayList&lt;String&gt; strList = new ArrayList&lt;String&gt;();
2strList.add(&quot;Mary had a little lamb named Willy&quot;);
3strList.add(&quot;Mary had a little ham&quot;);
4strList.add(&quot;Old McDonald had a farm named Willy&quot;);
5strList.add(&quot;Willy had a little dog named ham&quot;);
6strList.add(&quot;(abc)&quot;);
7strList.add(&quot;(xyz)&quot;);
8strList.add(&quot;Visit Target Store&quot;);
9strList.add(&quot;Visit Walmart Store&quot;);
10Mary had a -&gt; 2
11Mary had a little -&gt; 2
12( -&gt; 2
13Visit -&gt; 2
14named Willy -&gt; 2
15ham -&gt; 2
16) -&gt; 2
17Store -&gt; 2
18public static final int INDEX_NOT_FOUND = -1;
19
20public static String getAllCommonPrefixesInList(final String... strs) {
21    if (strs == null || strs.length == 0) {
22        return EMPTY_STRING;
23    }
24    
25    
26    final int smallestIndexOfDiff = getIndexOfDifference(strs);
27    if (smallestIndexOfDiff == INDEX_NOT_FOUND) {
28        
29        // All Strings are identical
30        if (strs[0] == null) {
31            return EMPTY_STRING;
32        }
33        return strs[0];
34    } else if (smallestIndexOfDiff == 0) {
35        
36        
37        // No common initial characters found, return empty String
38        return EMPTY_STRING;
39    } else {
40        
41        // Common initial character sequence found, return sequence
42        return strs[0].substring(0, smallestIndexOfDiff);
43    }
44}
45
46
47
48
49
50
51public static int getIndexOfDifference(final CharSequence... charSequence) {
52    if (charSequence == null || charSequence.length &lt;= 1) {
53        return INDEX_NOT_FOUND;
54    }
55    boolean isAnyStringNull = false;
56    boolean areAllStringsNull = true;
57    
58    
59    final int arrayLen = charSequence.length;
60    int shortestStrLen = Integer.MAX_VALUE;
61    int longestStrLen = 0;
62
63    // Find the min and max string lengths - avoids having to check that we are not exceeding the length of the string each time through the bottom loop.
64    for (int i = 0; i &lt; arrayLen; i++) {
65        if (charSequence[i] == null) {
66            isAnyStringNull = true;
67            shortestStrLen = 0;
68        } else {
69            areAllStringsNull = false;
70            shortestStrLen = Math.min(charSequence[i].length(), shortestStrLen);
71            longestStrLen = Math.max(charSequence[i].length(), longestStrLen);
72        }
73    }
74
75    // Deals with lists containing all nulls or all empty strings
76    
77    if (areAllStringsNull || longestStrLen == 0 &amp;&amp; !isAnyStringNull) {
78        return INDEX_NOT_FOUND;
79    }
80
81    // Handle lists containing some nulls or some empty strings
82    if (shortestStrLen == 0) {
83        return 0;
84    }
85
86    // Find the position with the first difference across all strings
87    int firstDiff = -1;
88    for (int stringPos = 0; stringPos &lt; shortestStrLen; stringPos++) {
89        final char comparisonChar = charSequence[0].charAt(stringPos);
90        for (int arrayPos = 1; arrayPos &lt; arrayLen; arrayPos++) {
91            if (charSequence[arrayPos].charAt(stringPos) != comparisonChar) {
92                firstDiff = stringPos;
93                break;
94            }
95        }
96        if (firstDiff != -1) {
97            break;
98        }
99    }
100
101    if (firstDiff == -1 &amp;&amp; shortestStrLen != longestStrLen) {
102        
103        // We compared all of the characters up to the length of the
104        // shortest string and didn't find a match, but the string lengths
105        // vary, so return the length of the shortest string.
106        return shortestStrLen;
107    }
108    return firstDiff;
109}
110

However, my goal is to include any prefix/suffix with at least 2+ occurrences into the resulting map.

How can this be achieved with Java?

ANSWER

Answered 2022-Mar-22 at 07:13

This problem should be solved easily using a trie.

The trie node should basically keep a track of 2 things:

  1. Child nodes
  2. Count of prefixes ending at current node

Insert all strings in the trie, which will be done in O(string length * number of strings). After that, simply traversing the trie, you can hash the prefixes based on the count as per your use case. For suffixes, you can use the same approach, just start traversing the strings in reverse order.

Edit:
On second thought, trie might be the most efficient way, but a simple hashmap implementation should also work here. Here's an example to generate all prefixes with count > 1.

1ArrayList&lt;String&gt; strList = new ArrayList&lt;String&gt;();
2strList.add(&quot;Mary had a little lamb named Willy&quot;);
3strList.add(&quot;Mary had a little ham&quot;);
4strList.add(&quot;Old McDonald had a farm named Willy&quot;);
5strList.add(&quot;Willy had a little dog named ham&quot;);
6strList.add(&quot;(abc)&quot;);
7strList.add(&quot;(xyz)&quot;);
8strList.add(&quot;Visit Target Store&quot;);
9strList.add(&quot;Visit Walmart Store&quot;);
10Mary had a -&gt; 2
11Mary had a little -&gt; 2
12( -&gt; 2
13Visit -&gt; 2
14named Willy -&gt; 2
15ham -&gt; 2
16) -&gt; 2
17Store -&gt; 2
18public static final int INDEX_NOT_FOUND = -1;
19
20public static String getAllCommonPrefixesInList(final String... strs) {
21    if (strs == null || strs.length == 0) {
22        return EMPTY_STRING;
23    }
24    
25    
26    final int smallestIndexOfDiff = getIndexOfDifference(strs);
27    if (smallestIndexOfDiff == INDEX_NOT_FOUND) {
28        
29        // All Strings are identical
30        if (strs[0] == null) {
31            return EMPTY_STRING;
32        }
33        return strs[0];
34    } else if (smallestIndexOfDiff == 0) {
35        
36        
37        // No common initial characters found, return empty String
38        return EMPTY_STRING;
39    } else {
40        
41        // Common initial character sequence found, return sequence
42        return strs[0].substring(0, smallestIndexOfDiff);
43    }
44}
45
46
47
48
49
50
51public static int getIndexOfDifference(final CharSequence... charSequence) {
52    if (charSequence == null || charSequence.length &lt;= 1) {
53        return INDEX_NOT_FOUND;
54    }
55    boolean isAnyStringNull = false;
56    boolean areAllStringsNull = true;
57    
58    
59    final int arrayLen = charSequence.length;
60    int shortestStrLen = Integer.MAX_VALUE;
61    int longestStrLen = 0;
62
63    // Find the min and max string lengths - avoids having to check that we are not exceeding the length of the string each time through the bottom loop.
64    for (int i = 0; i &lt; arrayLen; i++) {
65        if (charSequence[i] == null) {
66            isAnyStringNull = true;
67            shortestStrLen = 0;
68        } else {
69            areAllStringsNull = false;
70            shortestStrLen = Math.min(charSequence[i].length(), shortestStrLen);
71            longestStrLen = Math.max(charSequence[i].length(), longestStrLen);
72        }
73    }
74
75    // Deals with lists containing all nulls or all empty strings
76    
77    if (areAllStringsNull || longestStrLen == 0 &amp;&amp; !isAnyStringNull) {
78        return INDEX_NOT_FOUND;
79    }
80
81    // Handle lists containing some nulls or some empty strings
82    if (shortestStrLen == 0) {
83        return 0;
84    }
85
86    // Find the position with the first difference across all strings
87    int firstDiff = -1;
88    for (int stringPos = 0; stringPos &lt; shortestStrLen; stringPos++) {
89        final char comparisonChar = charSequence[0].charAt(stringPos);
90        for (int arrayPos = 1; arrayPos &lt; arrayLen; arrayPos++) {
91            if (charSequence[arrayPos].charAt(stringPos) != comparisonChar) {
92                firstDiff = stringPos;
93                break;
94            }
95        }
96        if (firstDiff != -1) {
97            break;
98        }
99    }
100
101    if (firstDiff == -1 &amp;&amp; shortestStrLen != longestStrLen) {
102        
103        // We compared all of the characters up to the length of the
104        // shortest string and didn't find a match, but the string lengths
105        // vary, so return the length of the shortest string.
106        return shortestStrLen;
107    }
108    return firstDiff;
109}
110import java.util.*;
111import java.util.stream.*;
112
113class Main {
114  public static void main(String[] args) {
115    
116    System.out.println(&quot;Hello world!&quot;);
117
118    ArrayList&lt;String&gt; strList = new ArrayList&lt;String&gt;();
119    
120    strList.add(&quot;Mary had a little lamb named Willy&quot;);
121    strList.add(&quot;Mary had a little ham&quot;);
122    strList.add(&quot;Old McDonald had a farm named Willy&quot;);
123    strList.add(&quot;Willy had a little dog named ham&quot;);
124    strList.add(&quot;(abc)&quot;);
125    strList.add(&quot;(xyz)&quot;);
126    strList.add(&quot;Visit Target Store&quot;);
127    strList.add(&quot;Visit Walmart Store&quot;);
128
129    Map&lt;String, Integer&gt; prefixMap = new HashMap&lt;String, Integer&gt;();
130    ArrayList&lt;String&gt; stringsWithHighestOccurrence = new ArrayList&lt;String&gt;();
131
132    for (String word : strList) {
133            for (int i = 1; i &lt;= word.length(); i++){
134        String prefix = word.substring(0, i);
135        prefixMap.merge(prefix, 1, Integer::sum);
136      }
137        }
138
139    Integer maxval = Collections.max(prefixMap.values());
140
141    for (String key: prefixMap.keySet()){
142      Integer value = prefixMap.get(key);
143      if (value &gt; 1) System.out.println(key + &quot; : &quot; + value);
144      if (value == maxval) stringsWithHighestOccurrence.add(key);
145    }
146
147    int maxLength = stringsWithHighestOccurrence.stream().map(String::length).max(Integer::compareTo).get();
148    
149    System.out.println(maxLength);
150
151    ArrayList&lt;String&gt; prefixesWithMaxLength =
152stringsWithHighestOccurrence.stream().filter(c -&gt; c.length() == maxLength).collect(Collectors.toCollection(ArrayList::new));
153    System.out.println(prefixesWithMaxLength);
154  }
155}
156

Nonetheless, I'll also add a basic TrieNode implementation for the sake of completion, since my answer proposed that approach in the first place.

TrieNode:

1ArrayList&lt;String&gt; strList = new ArrayList&lt;String&gt;();
2strList.add(&quot;Mary had a little lamb named Willy&quot;);
3strList.add(&quot;Mary had a little ham&quot;);
4strList.add(&quot;Old McDonald had a farm named Willy&quot;);
5strList.add(&quot;Willy had a little dog named ham&quot;);
6strList.add(&quot;(abc)&quot;);
7strList.add(&quot;(xyz)&quot;);
8strList.add(&quot;Visit Target Store&quot;);
9strList.add(&quot;Visit Walmart Store&quot;);
10Mary had a -&gt; 2
11Mary had a little -&gt; 2
12( -&gt; 2
13Visit -&gt; 2
14named Willy -&gt; 2
15ham -&gt; 2
16) -&gt; 2
17Store -&gt; 2
18public static final int INDEX_NOT_FOUND = -1;
19
20public static String getAllCommonPrefixesInList(final String... strs) {
21    if (strs == null || strs.length == 0) {
22        return EMPTY_STRING;
23    }
24    
25    
26    final int smallestIndexOfDiff = getIndexOfDifference(strs);
27    if (smallestIndexOfDiff == INDEX_NOT_FOUND) {
28        
29        // All Strings are identical
30        if (strs[0] == null) {
31            return EMPTY_STRING;
32        }
33        return strs[0];
34    } else if (smallestIndexOfDiff == 0) {
35        
36        
37        // No common initial characters found, return empty String
38        return EMPTY_STRING;
39    } else {
40        
41        // Common initial character sequence found, return sequence
42        return strs[0].substring(0, smallestIndexOfDiff);
43    }
44}
45
46
47
48
49
50
51public static int getIndexOfDifference(final CharSequence... charSequence) {
52    if (charSequence == null || charSequence.length &lt;= 1) {
53        return INDEX_NOT_FOUND;
54    }
55    boolean isAnyStringNull = false;
56    boolean areAllStringsNull = true;
57    
58    
59    final int arrayLen = charSequence.length;
60    int shortestStrLen = Integer.MAX_VALUE;
61    int longestStrLen = 0;
62
63    // Find the min and max string lengths - avoids having to check that we are not exceeding the length of the string each time through the bottom loop.
64    for (int i = 0; i &lt; arrayLen; i++) {
65        if (charSequence[i] == null) {
66            isAnyStringNull = true;
67            shortestStrLen = 0;
68        } else {
69            areAllStringsNull = false;
70            shortestStrLen = Math.min(charSequence[i].length(), shortestStrLen);
71            longestStrLen = Math.max(charSequence[i].length(), longestStrLen);
72        }
73    }
74
75    // Deals with lists containing all nulls or all empty strings
76    
77    if (areAllStringsNull || longestStrLen == 0 &amp;&amp; !isAnyStringNull) {
78        return INDEX_NOT_FOUND;
79    }
80
81    // Handle lists containing some nulls or some empty strings
82    if (shortestStrLen == 0) {
83        return 0;
84    }
85
86    // Find the position with the first difference across all strings
87    int firstDiff = -1;
88    for (int stringPos = 0; stringPos &lt; shortestStrLen; stringPos++) {
89        final char comparisonChar = charSequence[0].charAt(stringPos);
90        for (int arrayPos = 1; arrayPos &lt; arrayLen; arrayPos++) {
91            if (charSequence[arrayPos].charAt(stringPos) != comparisonChar) {
92                firstDiff = stringPos;
93                break;
94            }
95        }
96        if (firstDiff != -1) {
97            break;
98        }
99    }
100
101    if (firstDiff == -1 &amp;&amp; shortestStrLen != longestStrLen) {
102        
103        // We compared all of the characters up to the length of the
104        // shortest string and didn't find a match, but the string lengths
105        // vary, so return the length of the shortest string.
106        return shortestStrLen;
107    }
108    return firstDiff;
109}
110import java.util.*;
111import java.util.stream.*;
112
113class Main {
114  public static void main(String[] args) {
115    
116    System.out.println(&quot;Hello world!&quot;);
117
118    ArrayList&lt;String&gt; strList = new ArrayList&lt;String&gt;();
119    
120    strList.add(&quot;Mary had a little lamb named Willy&quot;);
121    strList.add(&quot;Mary had a little ham&quot;);
122    strList.add(&quot;Old McDonald had a farm named Willy&quot;);
123    strList.add(&quot;Willy had a little dog named ham&quot;);
124    strList.add(&quot;(abc)&quot;);
125    strList.add(&quot;(xyz)&quot;);
126    strList.add(&quot;Visit Target Store&quot;);
127    strList.add(&quot;Visit Walmart Store&quot;);
128
129    Map&lt;String, Integer&gt; prefixMap = new HashMap&lt;String, Integer&gt;();
130    ArrayList&lt;String&gt; stringsWithHighestOccurrence = new ArrayList&lt;String&gt;();
131
132    for (String word : strList) {
133            for (int i = 1; i &lt;= word.length(); i++){
134        String prefix = word.substring(0, i);
135        prefixMap.merge(prefix, 1, Integer::sum);
136      }
137        }
138
139    Integer maxval = Collections.max(prefixMap.values());
140
141    for (String key: prefixMap.keySet()){
142      Integer value = prefixMap.get(key);
143      if (value &gt; 1) System.out.println(key + &quot; : &quot; + value);
144      if (value == maxval) stringsWithHighestOccurrence.add(key);
145    }
146
147    int maxLength = stringsWithHighestOccurrence.stream().map(String::length).max(Integer::compareTo).get();
148    
149    System.out.println(maxLength);
150
151    ArrayList&lt;String&gt; prefixesWithMaxLength =
152stringsWithHighestOccurrence.stream().filter(c -&gt; c.length() == maxLength).collect(Collectors.toCollection(ArrayList::new));
153    System.out.println(prefixesWithMaxLength);
154  }
155}
156class TrieNode {
157    private final Map&lt;Character, TrieNode&gt; children = new HashMap&lt;&gt;();
158    private int count;
159
160    Map&lt;Character, TrieNode&gt; getChildren() {
161        return children;
162    }
163
164    boolean getCount() {
165        return count;
166    }
167
168    void increaseCount() {
169        this.count += 1;
170    }
171}
172

Trie:

1ArrayList&lt;String&gt; strList = new ArrayList&lt;String&gt;();
2strList.add(&quot;Mary had a little lamb named Willy&quot;);
3strList.add(&quot;Mary had a little ham&quot;);
4strList.add(&quot;Old McDonald had a farm named Willy&quot;);
5strList.add(&quot;Willy had a little dog named ham&quot;);
6strList.add(&quot;(abc)&quot;);
7strList.add(&quot;(xyz)&quot;);
8strList.add(&quot;Visit Target Store&quot;);
9strList.add(&quot;Visit Walmart Store&quot;);
10Mary had a -&gt; 2
11Mary had a little -&gt; 2
12( -&gt; 2
13Visit -&gt; 2
14named Willy -&gt; 2
15ham -&gt; 2
16) -&gt; 2
17Store -&gt; 2
18public static final int INDEX_NOT_FOUND = -1;
19
20public static String getAllCommonPrefixesInList(final String... strs) {
21    if (strs == null || strs.length == 0) {
22        return EMPTY_STRING;
23    }
24    
25    
26    final int smallestIndexOfDiff = getIndexOfDifference(strs);
27    if (smallestIndexOfDiff == INDEX_NOT_FOUND) {
28        
29        // All Strings are identical
30        if (strs[0] == null) {
31            return EMPTY_STRING;
32        }
33        return strs[0];
34    } else if (smallestIndexOfDiff == 0) {
35        
36        
37        // No common initial characters found, return empty String
38        return EMPTY_STRING;
39    } else {
40        
41        // Common initial character sequence found, return sequence
42        return strs[0].substring(0, smallestIndexOfDiff);
43    }
44}
45
46
47
48
49
50
51public static int getIndexOfDifference(final CharSequence... charSequence) {
52    if (charSequence == null || charSequence.length &lt;= 1) {
53        return INDEX_NOT_FOUND;
54    }
55    boolean isAnyStringNull = false;
56    boolean areAllStringsNull = true;
57    
58    
59    final int arrayLen = charSequence.length;
60    int shortestStrLen = Integer.MAX_VALUE;
61    int longestStrLen = 0;
62
63    // Find the min and max string lengths - avoids having to check that we are not exceeding the length of the string each time through the bottom loop.
64    for (int i = 0; i &lt; arrayLen; i++) {
65        if (charSequence[i] == null) {
66            isAnyStringNull = true;
67            shortestStrLen = 0;
68        } else {
69            areAllStringsNull = false;
70            shortestStrLen = Math.min(charSequence[i].length(), shortestStrLen);
71            longestStrLen = Math.max(charSequence[i].length(), longestStrLen);
72        }
73    }
74
75    // Deals with lists containing all nulls or all empty strings
76    
77    if (areAllStringsNull || longestStrLen == 0 &amp;&amp; !isAnyStringNull) {
78        return INDEX_NOT_FOUND;
79    }
80
81    // Handle lists containing some nulls or some empty strings
82    if (shortestStrLen == 0) {
83        return 0;
84    }
85
86    // Find the position with the first difference across all strings
87    int firstDiff = -1;
88    for (int stringPos = 0; stringPos &lt; shortestStrLen; stringPos++) {
89        final char comparisonChar = charSequence[0].charAt(stringPos);
90        for (int arrayPos = 1; arrayPos &lt; arrayLen; arrayPos++) {
91            if (charSequence[arrayPos].charAt(stringPos) != comparisonChar) {
92                firstDiff = stringPos;
93                break;
94            }
95        }
96        if (firstDiff != -1) {
97            break;
98        }
99    }
100
101    if (firstDiff == -1 &amp;&amp; shortestStrLen != longestStrLen) {
102        
103        // We compared all of the characters up to the length of the
104        // shortest string and didn't find a match, but the string lengths
105        // vary, so return the length of the shortest string.
106        return shortestStrLen;
107    }
108    return firstDiff;
109}
110import java.util.*;
111import java.util.stream.*;
112
113class Main {
114  public static void main(String[] args) {
115    
116    System.out.println(&quot;Hello world!&quot;);
117
118    ArrayList&lt;String&gt; strList = new ArrayList&lt;String&gt;();
119    
120    strList.add(&quot;Mary had a little lamb named Willy&quot;);
121    strList.add(&quot;Mary had a little ham&quot;);
122    strList.add(&quot;Old McDonald had a farm named Willy&quot;);
123    strList.add(&quot;Willy had a little dog named ham&quot;);
124    strList.add(&quot;(abc)&quot;);
125    strList.add(&quot;(xyz)&quot;);
126    strList.add(&quot;Visit Target Store&quot;);
127    strList.add(&quot;Visit Walmart Store&quot;);
128
129    Map&lt;String, Integer&gt; prefixMap = new HashMap&lt;String, Integer&gt;();
130    ArrayList&lt;String&gt; stringsWithHighestOccurrence = new ArrayList&lt;String&gt;();
131
132    for (String word : strList) {
133            for (int i = 1; i &lt;= word.length(); i++){
134        String prefix = word.substring(0, i);
135        prefixMap.merge(prefix, 1, Integer::sum);
136      }
137        }
138
139    Integer maxval = Collections.max(prefixMap.values());
140
141    for (String key: prefixMap.keySet()){
142      Integer value = prefixMap.get(key);
143      if (value &gt; 1) System.out.println(key + &quot; : &quot; + value);
144      if (value == maxval) stringsWithHighestOccurrence.add(key);
145    }
146
147    int maxLength = stringsWithHighestOccurrence.stream().map(String::length).max(Integer::compareTo).get();
148    
149    System.out.println(maxLength);
150
151    ArrayList&lt;String&gt; prefixesWithMaxLength =
152stringsWithHighestOccurrence.stream().filter(c -&gt; c.length() == maxLength).collect(Collectors.toCollection(ArrayList::new));
153    System.out.println(prefixesWithMaxLength);
154  }
155}
156class TrieNode {
157    private final Map&lt;Character, TrieNode&gt; children = new HashMap&lt;&gt;();
158    private int count;
159
160    Map&lt;Character, TrieNode&gt; getChildren() {
161        return children;
162    }
163
164    boolean getCount() {
165        return count;
166    }
167
168    void increaseCount() {
169        this.count += 1;
170    }
171}
172class Trie {
173    private TrieNode root;
174
175    Trie() {
176        root = new TrieNode();
177    }
178
179    void insert(String word) {
180        TrieNode current = root;
181
182        for (char l : word.toCharArray()) {
183            current = current.getChildren().computeIfAbsent(l, c -&gt; new TrieNode());
184            current.increaseCount()
185        }
186    }
187}
188

Traversing the trie would be analogous to a simple DFS scenario where the "path" upto current node is also maintained (we're switching the path with the prefix upto this point).

Source https://stackoverflow.com/questions/71463016

QUESTION

Differences in floating point between JDK 8 and JDK 13

Asked 2022-Mar-20 at 18:16

It seems that JDK 8 and JDK 13 have different floating points.
I get on JDK 8, using Math:

1cos(2.3) = -0.666276021279824
2

And on JDK 13:

1cos(2.3) = -0.666276021279824
2cos(2.3) = -0.6662760212798241
3

How does this happen? Difference shows on 11th Gen Intel and on AMD Ryzen using Windows 10.

Edit 20.03.2022:
Using Long.toHexString(Double.doubleToRawLongBits()) I get different bit patterns:
I get on JDK 8:

1cos(2.3) = -0.666276021279824
2cos(2.3) = -0.6662760212798241
3cos(2.3) = 0xbfe5522217302fe0
4

And I get on JDK 13:

1cos(2.3) = -0.666276021279824
2cos(2.3) = -0.6662760212798241
3cos(2.3) = 0xbfe5522217302fe0
4cos(2.3) = 0xbfe5522217302fe1
5

ANSWER

Answered 2022-Mar-20 at 18:16

This seems to be caused by a JVM intrinsic function for Math.cos, which is described in the related issue JDK-8242461. The behavior experienced there is not considered an issue:

The returned results reported in this bug are indeed adjacent floating-point values [this is the case here as well]

[...]

Therefore, while it is possible one or the other of the returned values is outside of the accuracy bounds, just have different return values for Math.cos is not in and of itself evidence of a problem.

For reproducible results, use the StrictMath.cos instead.

And indeed, disabling the intrinsics using -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_dcos (as proposed in the linked issue), causes Math.cos to have the same (expected) result as StrictMath.cos.

So it appears the behavior you are seeing here is most likely compliant with the Math documentation as well.

Source https://stackoverflow.com/questions/71548717

QUESTION

Using NativeCall to call the C fn `erf` gets more precise output than `erf` in C

Asked 2022-Mar-06 at 09:09

I have written a Raku script to call erf function in C standard library:

1use NativeCall;
2sub erf(num64) returns num64 is native { * };
3
4say [0.5,1,2,3,4,-0.9].map: {erf($_.Num)};
5

The output of this script

1use NativeCall;
2sub erf(num64) returns num64 is native { * };
3
4say [0.5,1,2,3,4,-0.9].map: {erf($_.Num)};
5(0.5204998778130465 0.8427007929497149 0.9953222650189527 0.9999779095030014 0.9999999845827421 -0.7969082124228322)
6

matches with the output from C for all values [0.5,1,2,3,4,-0.9] except 4.

For 4 the C outputs 1.000000 while Raku gives 0.9999999845827421.

To test the output for 4 in C, run this code:

1use NativeCall;
2sub erf(num64) returns num64 is native { * };
3
4say [0.5,1,2,3,4,-0.9].map: {erf($_.Num)};
5(0.5204998778130465 0.8427007929497149 0.9953222650189527 0.9999779095030014 0.9999999845827421 -0.7969082124228322)
6#include &lt;stdio.h&gt; // Including header file for printf function
7#include &lt;math.h&gt;  // Including header file for erf function
8
9int main (){
10
11  double param, result;
12  param = 4.0;
13  result = erf(param);
14  printf(&quot;erf (%f) = %f\n&quot;, param, result);
15  return 0;
16}
17

Any idea what's going on? I need to output 1.0 from Raku too.

ANSWER

Answered 2022-Feb-04 at 12:56

For the C code, change %f to %.99g to show more digits. This reveals erf(4) returns 0.9999999845827420852373279558378271758556365966796875.

%f requests six digits after the decimal point. The value is rounded to fit that format. %.numberf requests number digits after the decimal point and always used the “fixed” format. %.numberg requests number significant digits and uses a a “general” format that switches to exponential notation when appropriate.

For the Raku code, if you want output of “1.0” or “1.000000”, you will need to apply some formatting request to the output. I do not practice Raku, but a brief search shows Raku has printf-like features you can use, so requesting the %f format with it should duplicate the C output.

Source https://stackoverflow.com/questions/70986616

QUESTION

Repeatedly removing the maximum average subarray

Asked 2022-Feb-28 at 18:19

I have an array of positive integers. For example:

1[1, 7, 8, 4, 2, 1, 4]
2

A "reduction operation" finds the array prefix with the highest average, and deletes it. Here, an array prefix means a contiguous subarray whose left end is the start of the array, such as [1] or [1, 7] or [1, 7, 8] above. Ties are broken by taking the longer prefix.

1[1, 7, 8, 4, 2, 1, 4]
2Original array:  [  1,   7,   8,   4,   2,   1,   4]
3
4Prefix averages: [1.0, 4.0, 5.3, 5.0, 4.4, 3.8, 3.9]
5
6-&gt; Delete [1, 7, 8], with maximum average 5.3
7-&gt; New array -&gt; [4, 2, 1, 4]
8

I will repeat the reduction operation until the array is empty:

1[1, 7, 8, 4, 2, 1, 4]
2Original array:  [  1,   7,   8,   4,   2,   1,   4]
3
4Prefix averages: [1.0, 4.0, 5.3, 5.0, 4.4, 3.8, 3.9]
5
6-&gt; Delete [1, 7, 8], with maximum average 5.3
7-&gt; New array -&gt; [4, 2, 1, 4]
8[1, 7, 8, 4, 2, 1, 4]
9^       ^
10[4, 2, 1, 4]
11^ ^
12[2, 1, 4]
13^       ^
14[]
15

Now, actually performing these array modifications isn't necessary; I'm only looking for the list of lengths of prefixes that would be deleted by this process, for example, [3, 1, 3] above.

What is an efficient algorithm for computing these prefix lengths?


The naive approach is to recompute all sums and averages from scratch in every iteration for an O(n^2) algorithm-- I've attached Python code for this below. I'm looking for any improvement on this approach-- most preferably, any solution below O(n^2), but an algorithm with the same complexity but better constant factors would also be helpful.

Here are a few of the things I've tried (without success):

  1. Dynamically maintaining prefix sums, for example with a Binary Indexed Tree. While I can easily update prefix sums or find a maximum prefix sum in O(log n) time, I haven't found any data structure which can update the average, as the denominator in the average is changing.
  2. Reusing the previous 'rankings' of prefix averages-- these rankings can change, e.g. in some array, the prefix ending at index 5 may have a larger average than the prefix ending at index 6, but after removing the first 3 elements, now the prefix ending at index 2 may have a smaller average than the one ending at 3.
  3. Looking for patterns in where prefixes end; for example, the rightmost element of any max average prefix is always a local maximum in the array, but it's not clear how much this helps.

This is a working Python implementation of the naive, quadratic method:

1[1, 7, 8, 4, 2, 1, 4]
2Original array:  [  1,   7,   8,   4,   2,   1,   4]
3
4Prefix averages: [1.0, 4.0, 5.3, 5.0, 4.4, 3.8, 3.9]
5
6-&gt; Delete [1, 7, 8], with maximum average 5.3
7-&gt; New array -&gt; [4, 2, 1, 4]
8[1, 7, 8, 4, 2, 1, 4]
9^       ^
10[4, 2, 1, 4]
11^ ^
12[2, 1, 4]
13^       ^
14[]
15from fractions import Fraction
16def find_array_reductions(nums: List[int]) -&gt; List[int]:
17    &quot;&quot;&quot;Return list of lengths of max average prefix reductions.&quot;&quot;&quot;
18
19    def max_prefix_avg(arr: List[int]) -&gt; Tuple[float, int]:
20        &quot;&quot;&quot;Return value and length of max average prefix in arr.&quot;&quot;&quot;
21        if len(arr) == 0:
22            return (-math.inf, 0)
23
24        best_length = 1
25        best_average = Fraction(0, 1)
26        running_sum = 0
27
28        for i, x in enumerate(arr, 1):
29            running_sum += x
30            new_average = Fraction(running_sum, i)
31            if new_average &gt;= best_average:
32                best_average = new_average
33                best_length = i
34
35        return (float(best_average), best_length)
36
37    removed_lengths = []
38    total_removed = 0
39
40    while total_removed &lt; len(nums):
41        _, new_removal = max_prefix_avg(nums[total_removed:])
42        removed_lengths.append(new_removal)
43        total_removed += new_removal
44
45    return removed_lengths
46

Edit: The originally published code had a rare error with large inputs from using Python's math.isclose() with default parameters for floating point comparison, rather than proper fraction comparison. This has been fixed in the current code. An example of the error can be found at this Try it online link, along with a foreword explaining exactly what causes this bug, if you're curious.

ANSWER

Answered 2022-Feb-27 at 22:44

This problem has a fun O(n) solution.

If you draw a graph of cumulative sum vs index, then:

The average value in the subarray between any two indexes is the slope of the line between those points on the graph.

The first highest-average-prefix will end at the point that makes the highest angle from 0. The next highest-average-prefix must then have a smaller average, and it will end at the point that makes the highest angle from the first ending. Continuing to the end of the array, we find that...

These segments of highest average are exactly the segments in the upper convex hull of the cumulative sum graph.

Find these segments using the monotone chain algorithm. Since the points are already sorted, it takes O(n) time.

1[1, 7, 8, 4, 2, 1, 4]
2Original array:  [  1,   7,   8,   4,   2,   1,   4]
3
4Prefix averages: [1.0, 4.0, 5.3, 5.0, 4.4, 3.8, 3.9]
5
6-&gt; Delete [1, 7, 8], with maximum average 5.3
7-&gt; New array -&gt; [4, 2, 1, 4]
8[1, 7, 8, 4, 2, 1, 4]
9^       ^
10[4, 2, 1, 4]
11^ ^
12[2, 1, 4]
13^       ^
14[]
15from fractions import Fraction
16def find_array_reductions(nums: List[int]) -&gt; List[int]:
17    &quot;&quot;&quot;Return list of lengths of max average prefix reductions.&quot;&quot;&quot;
18
19    def max_prefix_avg(arr: List[int]) -&gt; Tuple[float, int]:
20        &quot;&quot;&quot;Return value and length of max average prefix in arr.&quot;&quot;&quot;
21        if len(arr) == 0:
22            return (-math.inf, 0)
23
24        best_length = 1
25        best_average = Fraction(0, 1)
26        running_sum = 0
27
28        for i, x in enumerate(arr, 1):
29            running_sum += x
30            new_average = Fraction(running_sum, i)
31            if new_average &gt;= best_average:
32                best_average = new_average
33                best_length = i
34
35        return (float(best_average), best_length)
36
37    removed_lengths = []
38    total_removed = 0
39
40    while total_removed &lt; len(nums):
41        _, new_removal = max_prefix_avg(nums[total_removed:])
42        removed_lengths.append(new_removal)
43        total_removed += new_removal
44
45    return removed_lengths
46# Lengths of the segments in the upper convex hull
47# of the cumulative sum graph
48def upperSumHullLengths(arr):
49    if len(arr) &lt; 2:
50        if len(arr) &lt; 1:
51            return []
52        else:
53            return [1]
54    
55    hull = [(0, 0),(1, arr[0])]
56    for x in range(2, len(arr)+1):
57        # this has x coordinate x-1
58        prevPoint = hull[len(hull) - 1]
59        # next point in cumulative sum
60        point = (x, prevPoint[1] + arr[x-1])
61        # remove points not on the convex hull
62        while len(hull) &gt;= 2:
63            p0 = hull[len(hull)-2]
64            dx0 = prevPoint[0] - p0[0]
65            dy0 = prevPoint[1] - p0[1]
66            dx1 = x - prevPoint[0]
67            dy1 = point[1] - prevPoint[1]
68            if dy1*dx0 &lt; dy0*dx1:
69                break
70            hull.pop()
71            prevPoint = p0
72        hull.append(point)
73    
74    return [hull[i+1][0] - hull[i][0] for i in range(0, len(hull)-1)]
75
76
77print(upperSumHullLengths([  1,   7,   8,   4,   2,   1,   4]))
78

prints:

1[1, 7, 8, 4, 2, 1, 4]
2Original array:  [  1,   7,   8,   4,   2,   1,   4]
3
4Prefix averages: [1.0, 4.0, 5.3, 5.0, 4.4, 3.8, 3.9]
5
6-&gt; Delete [1, 7, 8], with maximum average 5.3
7-&gt; New array -&gt; [4, 2, 1, 4]
8[1, 7, 8, 4, 2, 1, 4]
9^       ^
10[4, 2, 1, 4]
11^ ^
12[2, 1, 4]
13^       ^
14[]
15from fractions import Fraction
16def find_array_reductions(nums: List[int]) -&gt; List[int]:
17    &quot;&quot;&quot;Return list of lengths of max average prefix reductions.&quot;&quot;&quot;
18
19    def max_prefix_avg(arr: List[int]) -&gt; Tuple[float, int]:
20        &quot;&quot;&quot;Return value and length of max average prefix in arr.&quot;&quot;&quot;
21        if len(arr) == 0:
22            return (-math.inf, 0)
23
24        best_length = 1
25        best_average = Fraction(0, 1)
26        running_sum = 0
27
28        for i, x in enumerate(arr, 1):
29            running_sum += x
30            new_average = Fraction(running_sum, i)
31            if new_average &gt;= best_average:
32                best_average = new_average
33                best_length = i
34
35        return (float(best_average), best_length)
36
37    removed_lengths = []
38    total_removed = 0
39
40    while total_removed &lt; len(nums):
41        _, new_removal = max_prefix_avg(nums[total_removed:])
42        removed_lengths.append(new_removal)
43        total_removed += new_removal
44
45    return removed_lengths
46# Lengths of the segments in the upper convex hull
47# of the cumulative sum graph
48def upperSumHullLengths(arr):
49    if len(arr) &lt; 2:
50        if len(arr) &lt; 1:
51            return []
52        else:
53            return [1]
54    
55    hull = [(0, 0),(1, arr[0])]
56    for x in range(2, len(arr)+1):
57        # this has x coordinate x-1
58        prevPoint = hull[len(hull) - 1]
59        # next point in cumulative sum
60        point = (x, prevPoint[1] + arr[x-1])
61        # remove points not on the convex hull
62        while len(hull) &gt;= 2:
63            p0 = hull[len(hull)-2]
64            dx0 = prevPoint[0] - p0[0]
65            dy0 = prevPoint[1] - p0[1]
66            dx1 = x - prevPoint[0]
67            dy1 = point[1] - prevPoint[1]
68            if dy1*dx0 &lt; dy0*dx1:
69                break
70            hull.pop()
71            prevPoint = p0
72        hull.append(point)
73    
74    return [hull[i+1][0] - hull[i][0] for i in range(0, len(hull)-1)]
75
76
77print(upperSumHullLengths([  1,   7,   8,   4,   2,   1,   4]))
78[3, 1, 3]
79

Source https://stackoverflow.com/questions/71287550

QUESTION

How do I calculate square root in Python?

Asked 2022-Feb-17 at 03:40

I need to calculate the square root of some numbers, for example √9 = 3 and √2 = 1.4142. How can I do it in Python?

The inputs will probably be all positive integers, and relatively small (say less than a billion), but just in case they're not, is there anything that might break?


Related

Note: This is an attempt at a canonical question after a discussion on Meta about an existing question with the same title.

ANSWER

Answered 2022-Feb-04 at 19:44
Option 1: math.sqrt()

The math module from the standard library has a sqrt function to calculate the square root of a number. It takes any type that can be converted to float (which includes int) as an argument and returns a float.

1&gt;&gt;&gt; import math
2&gt;&gt;&gt; math.sqrt(9)
33.0
4
Option 2: Fractional exponent

The power operator (**) or the built-in pow() function can also be used to calculate a square root. Mathematically speaking, the square root of a equals a to the power of 1/2.

The power operator requires numeric types and matches the conversion rules for binary arithmetic operators, so in this case it will return either a float or a complex number.

1&gt;&gt;&gt; import math
2&gt;&gt;&gt; math.sqrt(9)
33.0
4&gt;&gt;&gt; 9 ** (1/2)
53.0
6&gt;&gt;&gt; 9 ** .5  # Same thing
73.0
8&gt;&gt;&gt; 2 ** .5
91.4142135623730951
10

(Note: in Python 2, 1/2 is truncated to 0, so you have to force floating point arithmetic with 1.0/2 or similar. See Why does Python give the "wrong" answer for square root?)

This method can be generalized to nth root, though fractions that can't be exactly represented as a float (like 1/3 or any denominator that's not a power of 2) may cause some inaccuracy:

1&gt;&gt;&gt; import math
2&gt;&gt;&gt; math.sqrt(9)
33.0
4&gt;&gt;&gt; 9 ** (1/2)
53.0
6&gt;&gt;&gt; 9 ** .5  # Same thing
73.0
8&gt;&gt;&gt; 2 ** .5
91.4142135623730951
10&gt;&gt;&gt; 8 ** (1/3)
112.0
12&gt;&gt;&gt; 125 ** (1/3)
134.999999999999999
14
Edge cases Negative and complex

Exponentiation works with negative numbers and complex numbers, though the results have some slight inaccuracy:

1&gt;&gt;&gt; import math
2&gt;&gt;&gt; math.sqrt(9)
33.0
4&gt;&gt;&gt; 9 ** (1/2)
53.0
6&gt;&gt;&gt; 9 ** .5  # Same thing
73.0
8&gt;&gt;&gt; 2 ** .5
91.4142135623730951
10&gt;&gt;&gt; 8 ** (1/3)
112.0
12&gt;&gt;&gt; 125 ** (1/3)
134.999999999999999
14&gt;&gt;&gt; (-25) ** .5  # Should be 5j
15(3.061616997868383e-16+5j)
16&gt;&gt;&gt; 8j ** .5  # Should be 2+2j
17(2.0000000000000004+2j)
18

Note the parentheses on -25! Otherwise it's parsed as -(25**.5) because exponentiation is more tightly binding than unary negation.

Meanwhile, math is only built for floats, so for x<0, math.sqrt() will raise ValueError: math domain error and for complex x, it'll raise TypeError: can't convert complex to float. Instead, you can use cmath.sqrt(), which is more more accurate than exponentiation (and will likely be faster too):

1&gt;&gt;&gt; import math
2&gt;&gt;&gt; math.sqrt(9)
33.0
4&gt;&gt;&gt; 9 ** (1/2)
53.0
6&gt;&gt;&gt; 9 ** .5  # Same thing
73.0
8&gt;&gt;&gt; 2 ** .5
91.4142135623730951
10&gt;&gt;&gt; 8 ** (1/3)
112.0
12&gt;&gt;&gt; 125 ** (1/3)
134.999999999999999
14&gt;&gt;&gt; (-25) ** .5  # Should be 5j
15(3.061616997868383e-16+5j)
16&gt;&gt;&gt; 8j ** .5  # Should be 2+2j
17(2.0000000000000004+2j)
18&gt;&gt;&gt; import cmath
19&gt;&gt;&gt; cmath.sqrt(-25)
205j
21&gt;&gt;&gt; cmath.sqrt(8j)
22(2+2j)
23
Precision

Both options involve an implicit conversion to float, so floating point precision is a factor. For example:

1&gt;&gt;&gt; import math
2&gt;&gt;&gt; math.sqrt(9)
33.0
4&gt;&gt;&gt; 9 ** (1/2)
53.0
6&gt;&gt;&gt; 9 ** .5  # Same thing
73.0
8&gt;&gt;&gt; 2 ** .5
91.4142135623730951
10&gt;&gt;&gt; 8 ** (1/3)
112.0
12&gt;&gt;&gt; 125 ** (1/3)
134.999999999999999
14&gt;&gt;&gt; (-25) ** .5  # Should be 5j
15(3.061616997868383e-16+5j)
16&gt;&gt;&gt; 8j ** .5  # Should be 2+2j
17(2.0000000000000004+2j)
18&gt;&gt;&gt; import cmath
19&gt;&gt;&gt; cmath.sqrt(-25)
205j
21&gt;&gt;&gt; cmath.sqrt(8j)
22(2+2j)
23&gt;&gt;&gt; n = 10**30
24&gt;&gt;&gt; square = n**2
25&gt;&gt;&gt; x = square**.5
26&gt;&gt;&gt; x == n
27False
28&gt;&gt;&gt; x - n  # how far off are they?
290.0
30&gt;&gt;&gt; int(x) - n  # how far off is the float from the int?
3119884624838656
32

Very large numbers might not even fit in a float and you'll get OverflowError: int too large to convert to float. See Python sqrt limit for very large numbers?

Other types

Let's look at Decimal for example:

Exponentiation fails unless the exponent is also Decimal:

1&gt;&gt;&gt; import math
2&gt;&gt;&gt; math.sqrt(9)
33.0
4&gt;&gt;&gt; 9 ** (1/2)
53.0
6&gt;&gt;&gt; 9 ** .5  # Same thing
73.0
8&gt;&gt;&gt; 2 ** .5
91.4142135623730951
10&gt;&gt;&gt; 8 ** (1/3)
112.0
12&gt;&gt;&gt; 125 ** (1/3)
134.999999999999999
14&gt;&gt;&gt; (-25) ** .5  # Should be 5j
15(3.061616997868383e-16+5j)
16&gt;&gt;&gt; 8j ** .5  # Should be 2+2j
17(2.0000000000000004+2j)
18&gt;&gt;&gt; import cmath
19&gt;&gt;&gt; cmath.sqrt(-25)
205j
21&gt;&gt;&gt; cmath.sqrt(8j)
22(2+2j)
23&gt;&gt;&gt; n = 10**30
24&gt;&gt;&gt; square = n**2
25&gt;&gt;&gt; x = square**.5
26&gt;&gt;&gt; x == n
27False
28&gt;&gt;&gt; x - n  # how far off are they?
290.0
30&gt;&gt;&gt; int(x) - n  # how far off is the float from the int?
3119884624838656
32&gt;&gt;&gt; decimal.Decimal('9') ** .5
33Traceback (most recent call last):
34  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
35TypeError: unsupported operand type(s) for ** or pow(): 'decimal.Decimal' and 'float'
36&gt;&gt;&gt; decimal.Decimal('9') ** decimal.Decimal('.5')
37Decimal('3.000000000000000000000000000')
38

Meanwhile, math and cmath will silently convert their arguments to float and complex respectively, which could mean loss of precision.

decimal also has its own .sqrt(). See also calculating n-th roots using Python 3's decimal module

Source https://stackoverflow.com/questions/70793490

QUESTION

JMH using java 17, no dead code elimination

Asked 2022-Feb-09 at 17:17

I run sample JHM benchmark which suppose to show dead code elimination. Code is rewritten for conciseness from jhm github sample.

1import org.openjdk.jmh.annotations.*;
2import java.util.concurrent.TimeUnit;
3
4@State(Scope.Thread)
5@BenchmarkMode(Mode.AverageTime)
6@OutputTimeUnit(TimeUnit.NANOSECONDS)
7@Fork(1)
8public class Sample08DeadCode {
9
10    private double x = Math.PI;
11
12    @Benchmark
13    public void benchmark() {}
14
15    @Benchmark
16    public void measureIncorrect() { Math.log(x); }
17
18    @Benchmark
19    public double measureCorrect() { return Math.log(x); }
20}
21

Run using JDK 1.8.0_211, Java HotSpot(TM) 64-Bit Server VM, 25.211-b12 produces following results:

1import org.openjdk.jmh.annotations.*;
2import java.util.concurrent.TimeUnit;
3
4@State(Scope.Thread)
5@BenchmarkMode(Mode.AverageTime)
6@OutputTimeUnit(TimeUnit.NANOSECONDS)
7@Fork(1)
8public class Sample08DeadCode {
9
10    private double x = Math.PI;
11
12    @Benchmark
13    public void benchmark() {}
14
15    @Benchmark
16    public void measureIncorrect() { Math.log(x); }
17
18    @Benchmark
19    public double measureCorrect() { return Math.log(x); }
20}
21Benchmark                          Mode  Cnt   Score   Error  Units
22Sample08DeadCode.benchmark         avgt    5   0,229 ± 0,018  ns/op
23Sample08DeadCode.measureCorrect    avgt    5  12,013 ± 0,047  ns/op
24Sample08DeadCode.measureIncorrect  avgt    5   0,228 ± 0,016  ns/op
25

but using java JDK 17.0.2, Java HotSpot(TM) 64-Bit Server VM, 17.0.2+8-LTS-86 the results have no sign of dead code elimination:

1import org.openjdk.jmh.annotations.*;
2import java.util.concurrent.TimeUnit;
3
4@State(Scope.Thread)
5@BenchmarkMode(Mode.AverageTime)
6@OutputTimeUnit(TimeUnit.NANOSECONDS)
7@Fork(1)
8public class Sample08DeadCode {
9
10    private double x = Math.PI;
11
12    @Benchmark
13    public void benchmark() {}
14
15    @Benchmark
16    public void measureIncorrect() { Math.log(x); }
17
18    @Benchmark
19    public double measureCorrect() { return Math.log(x); }
20}
21Benchmark                          Mode  Cnt   Score   Error  Units
22Sample08DeadCode.benchmark         avgt    5   0,229 ± 0,018  ns/op
23Sample08DeadCode.measureCorrect    avgt    5  12,013 ± 0,047  ns/op
24Sample08DeadCode.measureIncorrect  avgt    5   0,228 ± 0,016  ns/op
25Benchmark                          Mode  Cnt  Score   Error  Units
26Sample08DeadCode.benchmark         avgt    5  0,341 ± 0,004  ns/op
27Sample08DeadCode.measureCorrect    avgt    5  6,244 ± 0,072  ns/op
28Sample08DeadCode.measureIncorrect  avgt    5  6,263 ± 0,094  ns/op
29

Why does the measureIncorrect() method is not optimized using java 17?

ANSWER

Answered 2022-Feb-09 at 17:17

Those samples depend on JDK internals.

Looks like since JDK 9 and JDK-8152907, Math.log is no longer intrinsified into C2 intermediate representation. Instead, a direct call to a quick LIBM-backed stub is made. This is usually faster for the code that actually uses the result. Notice how measureCorrect is faster in JDK 17 output in your case.

But for JMH samples, it limits the the compiler optimizations around the Math.log, and dead code / folding samples do not work properly. The fix it to make samples that do not rely on JDK internals without a good reason, and instead use a custom written payload.

This is being done in JMH here:

Source https://stackoverflow.com/questions/71044636

QUESTION

Docker standard_init_linux.go:228: exec user process caused: no such file or directory

Asked 2022-Feb-08 at 20:49

Whenever I am trying to run the docker images, it is exiting in immediately.

1CONTAINER ID   IMAGE                      COMMAND                CREATED          STATUS                      PORTS     NAMES
2ae327a2bdba3   k8s-for-beginners:v0.0.1   &quot;/k8s-for-beginners&quot;   11 seconds ago   Exited (1) 10 seconds ago             focused_booth
3

As per Container Logs

1CONTAINER ID   IMAGE                      COMMAND                CREATED          STATUS                      PORTS     NAMES
2ae327a2bdba3   k8s-for-beginners:v0.0.1   &quot;/k8s-for-beginners&quot;   11 seconds ago   Exited (1) 10 seconds ago             focused_booth
3standard_init_linux.go:228: exec user process caused: no such file or directory
4

I have created all the files in linux itself:

1CONTAINER ID   IMAGE                      COMMAND                CREATED          STATUS                      PORTS     NAMES
2ae327a2bdba3   k8s-for-beginners:v0.0.1   &quot;/k8s-for-beginners&quot;   11 seconds ago   Exited (1) 10 seconds ago             focused_booth
3standard_init_linux.go:228: exec user process caused: no such file or directory
4FROM alpine:3.10
5COPY k8s-for-beginners /
6CMD [&quot;/k8s-for-beginners&quot;]
7

GO Code:

1CONTAINER ID   IMAGE                      COMMAND                CREATED          STATUS                      PORTS     NAMES
2ae327a2bdba3   k8s-for-beginners:v0.0.1   &quot;/k8s-for-beginners&quot;   11 seconds ago   Exited (1) 10 seconds ago             focused_booth
3standard_init_linux.go:228: exec user process caused: no such file or directory
4FROM alpine:3.10
5COPY k8s-for-beginners /
6CMD [&quot;/k8s-for-beginners&quot;]
7package main
8
9import (
10    &quot;fmt&quot;
11    &quot;log&quot;
12    &quot;net/http&quot;
13)
14
15func main() {
16    http.HandleFunc(&quot;/&quot;, handler)
17    log.Fatal(http.ListenAndServe(&quot;0.0.0.0:8080&quot;, nil))
18}
19
20func handler(w http.ResponseWriter, r *http.Request) {
21    log.Printf(&quot;Ping from %s&quot;, r.RemoteAddr)
22    fmt.Fprintln(w, &quot;Hello Kubernetes Beginners!&quot;)
23}
24

This is the first exercise from THE KUBERNETES WORKSHOP book.

Commands I have used in this Process:

1CONTAINER ID   IMAGE                      COMMAND                CREATED          STATUS                      PORTS     NAMES
2ae327a2bdba3   k8s-for-beginners:v0.0.1   &quot;/k8s-for-beginners&quot;   11 seconds ago   Exited (1) 10 seconds ago             focused_booth
3standard_init_linux.go:228: exec user process caused: no such file or directory
4FROM alpine:3.10
5COPY k8s-for-beginners /
6CMD [&quot;/k8s-for-beginners&quot;]
7package main
8
9import (
10    &quot;fmt&quot;
11    &quot;log&quot;
12    &quot;net/http&quot;
13)
14
15func main() {
16    http.HandleFunc(&quot;/&quot;, handler)
17    log.Fatal(http.ListenAndServe(&quot;0.0.0.0:8080&quot;, nil))
18}
19
20func handler(w http.ResponseWriter, r *http.Request) {
21    log.Printf(&quot;Ping from %s&quot;, r.RemoteAddr)
22    fmt.Fprintln(w, &quot;Hello Kubernetes Beginners!&quot;)
23}
24CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o k8s-for-beginners
25sudo docker build -t k8s-for-beginners:v0.0.1 .
26sudo docker run -p 8080:8080 -d k8s-for-beginners:v0.0.1
27

Output of sudo docker run k8s-for-beginners:v0.0.1 ldd /k8s-for-beginners

1CONTAINER ID   IMAGE                      COMMAND                CREATED          STATUS                      PORTS     NAMES
2ae327a2bdba3   k8s-for-beginners:v0.0.1   &quot;/k8s-for-beginners&quot;   11 seconds ago   Exited (1) 10 seconds ago             focused_booth
3standard_init_linux.go:228: exec user process caused: no such file or directory
4FROM alpine:3.10
5COPY k8s-for-beginners /
6CMD [&quot;/k8s-for-beginners&quot;]
7package main
8
9import (
10    &quot;fmt&quot;
11    &quot;log&quot;
12    &quot;net/http&quot;
13)
14
15func main() {
16    http.HandleFunc(&quot;/&quot;, handler)
17    log.Fatal(http.ListenAndServe(&quot;0.0.0.0:8080&quot;, nil))
18}
19
20func handler(w http.ResponseWriter, r *http.Request) {
21    log.Printf(&quot;Ping from %s&quot;, r.RemoteAddr)
22    fmt.Fprintln(w, &quot;Hello Kubernetes Beginners!&quot;)
23}
24CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o k8s-for-beginners
25sudo docker build -t k8s-for-beginners:v0.0.1 .
26sudo docker run -p 8080:8080 -d k8s-for-beginners:v0.0.1
27        /lib64/ld-linux-x86-64.so.2 (0x7f9ab5778000)
28        libc.so.6 =&gt; /lib64/ld-linux-x86-64.so.2 (0x7f9ab5778000)
29Error loading shared library libgo.so.16: No such file or directory (needed by /k8s-for-beginners)
30Error loading shared library libgcc_s.so.1: No such file or directory (needed by /k8s-for-beginners)
31Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by /k8s-for-beginners)
32Error relocating /k8s-for-beginners: crypto..z2frsa..import: symbol not found
33Error relocating /k8s-for-beginners: crypto..z2fx509..import: symbol not found
34Error relocating /k8s-for-beginners: log..import: symbol not found
35Error relocating /k8s-for-beginners: crypto..z2fmd5..import: symbol not found
36Error relocating /k8s-for-beginners: crypto..import: symbol not found
37Error relocating /k8s-for-beginners: bytes..import: symbol not found
38Error relocating /k8s-for-beginners: fmt.Fprintln: symbol not found
39Error relocating /k8s-for-beginners: crypto..z2felliptic..import: symbol not found
40Error relocating /k8s-for-beginners: crypto..z2fx509..z2fpkix..import: symbol not found
41Error relocating /k8s-for-beginners: crypto..z2frand..import: symbol not found
42Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fchacha20poly1305..import: symbol not found
43Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcurve25519..import: symbol not found
44Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fidna..import: symbol not found
45Error relocating /k8s-for-beginners: internal..z2foserror..import: symbol not found
46Error relocating /k8s-for-beginners: crypto..z2fecdsa..import: symbol not found
47Error relocating /k8s-for-beginners: net..z2fhttp.HandleFunc: symbol not found
48Error relocating /k8s-for-beginners: io..import: symbol not found
49Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp2..z2fhpack..import: symbol not found
50Error relocating /k8s-for-beginners: crypto..z2fcipher..import: symbol not found
51Error relocating /k8s-for-beginners: log.Fatal: symbol not found
52Error relocating /k8s-for-beginners: math..z2fbig..import: symbol not found
53Error relocating /k8s-for-beginners: runtime..import: symbol not found
54Error relocating /k8s-for-beginners: net..z2fhttp..import: symbol not found
55Error relocating /k8s-for-beginners: hash..z2fcrc32..import: symbol not found
56Error relocating /k8s-for-beginners: net..z2fhttp.ListenAndServe: symbol not found
57Error relocating /k8s-for-beginners: context..import: symbol not found
58Error relocating /k8s-for-beginners: fmt..import: symbol not found
59Error relocating /k8s-for-beginners: crypto..z2ftls..import: symbol not found
60Error relocating /k8s-for-beginners: errors..import: symbol not found
61Error relocating /k8s-for-beginners: internal..z2ftestlog..import: symbol not found
62Error relocating /k8s-for-beginners: runtime.setIsCgo: symbol not found
63Error relocating /k8s-for-beginners: runtime_m: symbol not found
64Error relocating /k8s-for-beginners: encoding..z2fhex..import: symbol not found
65Error relocating /k8s-for-beginners: mime..import: symbol not found
66Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2funicode..z2fbidi..import: symbol not found
67Error relocating /k8s-for-beginners: internal..z2freflectlite..import: symbol not found
68Error relocating /k8s-for-beginners: compress..z2fgzip..import: symbol not found
69Error relocating /k8s-for-beginners: sync..import: symbol not found
70Error relocating /k8s-for-beginners: compress..z2fflate..import: symbol not found
71Error relocating /k8s-for-beginners: encoding..z2fbinary..import: symbol not found
72Error relocating /k8s-for-beginners: math..z2frand..import: symbol not found
73Error relocating /k8s-for-beginners: runtime_cpuinit: symbol not found
74Error relocating /k8s-for-beginners: internal..z2fpoll..import: symbol not found
75Error relocating /k8s-for-beginners: mime..z2fmultipart..import: symbol not found
76Error relocating /k8s-for-beginners: runtime.check: symbol not found
77Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcryptobyte..import: symbol not found
78Error relocating /k8s-for-beginners: crypto..z2fsha512..import: symbol not found
79Error relocating /k8s-for-beginners: runtime.registerTypeDescriptors: symbol not found
80Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fchacha20..import: symbol not found
81Error relocating /k8s-for-beginners: runtime.setmodinfo: symbol not found
82Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2ftransform..import: symbol not found
83Error relocating /k8s-for-beginners: time..import: symbol not found
84Error relocating /k8s-for-beginners: encoding..z2fbase64..import: symbol not found
85Error relocating /k8s-for-beginners: crypto..z2fsha256..import: symbol not found
86Error relocating /k8s-for-beginners: __go_go: symbol not found
87Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp..z2fhttpguts..import: symbol not found
88Error relocating /k8s-for-beginners: path..z2ffilepath..import: symbol not found
89Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2fsecure..z2fbidirule..import: symbol not found
90Error relocating /k8s-for-beginners: os..import: symbol not found
91Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp..z2fhttpproxy..import: symbol not found
92Error relocating /k8s-for-beginners: net..z2ftextproto..import: symbol not found
93Error relocating /k8s-for-beginners: encoding..z2fasn1..import: symbol not found
94Error relocating /k8s-for-beginners: runtime.requireitab: symbol not found
95Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fdns..z2fdnsmessage..import: symbol not found
96Error relocating /k8s-for-beginners: path..import: symbol not found
97Error relocating /k8s-for-beginners: io..z2fioutil..import: symbol not found
98Error relocating /k8s-for-beginners: sort..import: symbol not found
99Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2funicode..z2fnorm..import: symbol not found
100Error relocating /k8s-for-beginners: internal..z2fcpu..import: symbol not found
101Error relocating /k8s-for-beginners: runtime.ginit: symbol not found
102Error relocating /k8s-for-beginners: runtime.osinit: symbol not found
103Error relocating /k8s-for-beginners: runtime.schedinit: symbol not found
104Error relocating /k8s-for-beginners: bufio..import: symbol not found
105Error relocating /k8s-for-beginners: crypto..z2finternal..z2frandutil..import: symbol not found
106Error relocating /k8s-for-beginners: runtime_mstart: symbol not found
107Error relocating /k8s-for-beginners: net..import: symbol not found
108Error relocating /k8s-for-beginners: strconv..import: symbol not found
109Error relocating /k8s-for-beginners: runtime.args: symbol not found
110Error relocating /k8s-for-beginners: runtime..z2finternal..z2fsys..import: symbol not found
111Error relocating /k8s-for-beginners: runtime.newobject: symbol not found
112Error relocating /k8s-for-beginners: syscall..import: symbol not found
113Error relocating /k8s-for-beginners: unicode..import: symbol not found
114Error relocating /k8s-for-beginners: net..z2fhttp..z2finternal..import: symbol not found
115Error relocating /k8s-for-beginners: encoding..z2fpem..import: symbol not found
116Error relocating /k8s-for-beginners: _Unwind_Resume: symbol not found
117Error relocating /k8s-for-beginners: reflect..import: symbol not found
118Error relocating /k8s-for-beginners: mime..z2fquotedprintable..import: symbol not found
119Error relocating /k8s-for-beginners: log.Printf: symbol not found
120Error relocating /k8s-for-beginners: runtime.typedmemmove: symbol not found
121Error relocating /k8s-for-beginners: crypto..z2fdsa..import: symbol not found
122Error relocating /k8s-for-beginners: crypto..z2fsha1..import: symbol not found
123Error relocating /k8s-for-beginners: bufio..types: symbol not found
124Error relocating /k8s-for-beginners: bytes..types: symbol not found
125Error relocating /k8s-for-beginners: compress..z2fflate..types: symbol not found
126Error relocating /k8s-for-beginners: compress..z2fgzip..types: symbol not found
127Error relocating /k8s-for-beginners: context..types: symbol not found
128Error relocating /k8s-for-beginners: crypto..types: symbol not found
129Error relocating /k8s-for-beginners: crypto..z2fcipher..types: symbol not found
130Error relocating /k8s-for-beginners: crypto..z2fdsa..types: symbol not found
131Error relocating /k8s-for-beginners: crypto..z2fecdsa..types: symbol not found
132Error relocating /k8s-for-beginners: crypto..z2felliptic..types: symbol not found
133Error relocating /k8s-for-beginners: crypto..z2finternal..z2frandutil..types: symbol not found
134Error relocating /k8s-for-beginners: crypto..z2fmd5..types: symbol not found
135Error relocating /k8s-for-beginners: crypto..z2frand..types: symbol not found
136Error relocating /k8s-for-beginners: crypto..z2frsa..types: symbol not found
137Error relocating /k8s-for-beginners: crypto..z2fsha1..types: symbol not found
138Error relocating /k8s-for-beginners: crypto..z2fsha256..types: symbol not found
139Error relocating /k8s-for-beginners: crypto..z2fsha512..types: symbol not found
140Error relocating /k8s-for-beginners: crypto..z2ftls..types: symbol not found
141Error relocating /k8s-for-beginners: crypto..z2fx509..types: symbol not found
142Error relocating /k8s-for-beginners: crypto..z2fx509..z2fpkix..types: symbol not found
143Error relocating /k8s-for-beginners: encoding..z2fasn1..types: symbol not found
144Error relocating /k8s-for-beginners: encoding..z2fbase64..types: symbol not found
145Error relocating /k8s-for-beginners: encoding..z2fbinary..types: symbol not found
146Error relocating /k8s-for-beginners: encoding..z2fhex..types: symbol not found
147Error relocating /k8s-for-beginners: encoding..z2fpem..types: symbol not found
148Error relocating /k8s-for-beginners: errors..types: symbol not found
149Error relocating /k8s-for-beginners: fmt..types: symbol not found
150Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fchacha20..types: symbol not found
151Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fchacha20poly1305..types: symbol not found
152Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcryptobyte..types: symbol not found
153Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcurve25519..types: symbol not found
154Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fdns..z2fdnsmessage..types: symbol not found
155Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp..z2fhttpguts..types: symbol not found
156Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp..z2fhttpproxy..types: symbol not found
157Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp2..z2fhpack..types: symbol not found
158Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fidna..types: symbol not found
159Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2fsecure..z2fbidirule..types: symbol not found
160Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2ftransform..types: symbol not found
161Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2funicode..z2fbidi..types: symbol not found
162Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2funicode..z2fnorm..types: symbol not found
163Error relocating /k8s-for-beginners: hash..z2fcrc32..types: symbol not found
164Error relocating /k8s-for-beginners: internal..z2fcpu..types: symbol not found
165Error relocating /k8s-for-beginners: internal..z2foserror..types: symbol not found
166Error relocating /k8s-for-beginners: internal..z2fpoll..types: symbol not found
167Error relocating /k8s-for-beginners: internal..z2freflectlite..types: symbol not found
168Error relocating /k8s-for-beginners: internal..z2ftestlog..types: symbol not found
169Error relocating /k8s-for-beginners: io..types: symbol not found
170Error relocating /k8s-for-beginners: io..z2fioutil..types: symbol not found
171Error relocating /k8s-for-beginners: log..types: symbol not found
172Error relocating /k8s-for-beginners: math..z2fbig..types: symbol not found
173Error relocating /k8s-for-beginners: math..z2frand..types: symbol not found
174Error relocating /k8s-for-beginners: mime..types: symbol not found
175Error relocating /k8s-for-beginners: mime..z2fmultipart..types: symbol not found
176Error relocating /k8s-for-beginners: mime..z2fquotedprintable..types: symbol not found
177Error relocating /k8s-for-beginners: net..types: symbol not found
178Error relocating /k8s-for-beginners: net..z2fhttp..types: symbol not found
179Error relocating /k8s-for-beginners: net..z2fhttp..z2finternal..types: symbol not found
180Error relocating /k8s-for-beginners: net..z2ftextproto..types: symbol not found
181Error relocating /k8s-for-beginners: os..types: symbol not found
182Error relocating /k8s-for-beginners: path..types: symbol not found
183Error relocating /k8s-for-beginners: path..z2ffilepath..types: symbol not found
184Error relocating /k8s-for-beginners: reflect..types: symbol not found
185Error relocating /k8s-for-beginners: runtime..types: symbol not found
186Error relocating /k8s-for-beginners: runtime..z2finternal..z2fsys..types: symbol not found
187Error relocating /k8s-for-beginners: sort..types: symbol not found
188Error relocating /k8s-for-beginners: strconv..types: symbol not found
189Error relocating /k8s-for-beginners: sync..types: symbol not found
190Error relocating /k8s-for-beginners: syscall..types: symbol not found
191Error relocating /k8s-for-beginners: time..types: symbol not found
192Error relocating /k8s-for-beginners: unicode..types: symbol not found
193Error relocating /k8s-for-beginners: container..z2flist..types: symbol not found
194Error relocating /k8s-for-beginners: crypto..z2faes..types: symbol not found
195Error relocating /k8s-for-beginners: crypto..z2fdes..types: symbol not found
196Error relocating /k8s-for-beginners: crypto..z2fed25519..types: symbol not found
197Error relocating /k8s-for-beginners: crypto..z2fed25519..z2finternal..z2fedwards25519..types: symbol not found
198Error relocating /k8s-for-beginners: crypto..z2fhmac..types: symbol not found
199Error relocating /k8s-for-beginners: crypto..z2finternal..z2fsubtle..types: symbol not found
200Error relocating /k8s-for-beginners: crypto..z2frc4..types: symbol not found
201Error relocating /k8s-for-beginners: crypto..z2fsubtle..types: symbol not found
202Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcryptobyte..z2fasn1..types: symbol not found
203Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fhkdf..types: symbol not found
204Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2finternal..z2fsubtle..types: symbol not found
205Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fpoly1305..types: symbol not found
206Error relocating /k8s-for-beginners: hash..types: symbol not found
207Error relocating /k8s-for-beginners: internal..z2fbytealg..types: symbol not found
208Error relocating /k8s-for-beginners: internal..z2ffmtsort..types: symbol not found
209Error relocating /k8s-for-beginners: internal..z2fnettrace..types: symbol not found
210Error relocating /k8s-for-beginners: internal..z2frace..types: symbol not found
211Error relocating /k8s-for-beginners: internal..z2fsingleflight..types: symbol not found
212Error relocating /k8s-for-beginners: internal..z2fsyscall..z2fexecenv..types: symbol not found
213Error relocating /k8s-for-beginners: internal..z2fsyscall..z2funix..types: symbol not found
214Error relocating /k8s-for-beginners: math..types: symbol not found
215Error relocating /k8s-for-beginners: math..z2fbits..types: symbol not found
216Error relocating /k8s-for-beginners: net..z2fhttp..z2fhttptrace..types: symbol not found
217Error relocating /k8s-for-beginners: net..z2furl..types: symbol not found
218Error relocating /k8s-for-beginners: runtime..z2finternal..z2fatomic..types: symbol not found
219Error relocating /k8s-for-beginners: runtime..z2finternal..z2fmath..types: symbol not found
220Error relocating /k8s-for-beginners: strings..types: symbol not found
221Error relocating /k8s-for-beginners: sync..z2fatomic..types: symbol not found
222Error relocating /k8s-for-beginners: unicode..z2futf16..types: symbol not found
223Error relocating /k8s-for-beginners: unicode..z2futf8..types: symbol not found
224Error relocating /k8s-for-beginners: runtime.strequal..f: symbol not found
225Error relocating /k8s-for-beginners: runtime.memequal64..f: symbol not found
226Error relocating /k8s-for-beginners: type...1reflect.rtype: symbol not found
227Error relocating /k8s-for-beginners: reflect.rtype.Align: symbol not found
228Error relocating /k8s-for-beginners: reflect.rtype.Align: symbol not found
229Error relocating /k8s-for-beginners: reflect.rtype.AssignableTo: symbol not found
230Error relocating /k8s-for-beginners: reflect.rtype.AssignableTo: symbol not found
231Error relocating /k8s-for-beginners: reflect.rtype.Bits: symbol not found
232Error relocating /k8s-for-beginners: reflect.rtype.Bits: symbol not found
233Error relocating /k8s-for-beginners: reflect.rtype.ChanDir: symbol not found
234Error relocating /k8s-for-beginners: reflect.rtype.ChanDir: symbol not found
235Error relocating /k8s-for-beginners: reflect.rtype.Comparable: symbol not found
236Error relocating /k8s-for-beginners: reflect.rtype.Comparable: symbol not found
237Error relocating /k8s-for-beginners: reflect.rtype.ConvertibleTo: symbol not found
238Error relocating /k8s-for-beginners: reflect.rtype.ConvertibleTo: symbol not found
239Error relocating /k8s-for-beginners: reflect.rtype.Elem: symbol not found
240Error relocating /k8s-for-beginners: reflect.rtype.Elem: symbol not found
241Error relocating /k8s-for-beginners: reflect.rtype.Field: symbol not found
242Error relocating /k8s-for-beginners: reflect.rtype.Field: symbol not found
243Error relocating /k8s-for-beginners: reflect.rtype.FieldAlign: symbol not found
244Error relocating /k8s-for-beginners: reflect.rtype.FieldAlign: symbol not found
245Error relocating /k8s-for-beginners: reflect.rtype.FieldByIndex: symbol not found
246Error relocating /k8s-for-beginners: reflect.rtype.FieldByIndex: symbol not found
247Error relocating /k8s-for-beginners: reflect.rtype.FieldByName: symbol not found
248Error relocating /k8s-for-beginners: reflect.rtype.FieldByName: symbol not found
249Error relocating /k8s-for-beginners: reflect.rtype.FieldByNameFunc: symbol not found
250Error relocating /k8s-for-beginners: reflect.rtype.FieldByNameFunc: symbol not found
251Error relocating /k8s-for-beginners: reflect.rtype.Implements: symbol not found
252Error relocating /k8s-for-beginners: reflect.rtype.Implements: symbol not found
253Error relocating /k8s-for-beginners: reflect.rtype.In: symbol not found
254Error relocating /k8s-for-beginners: reflect.rtype.In: symbol not found
255Error relocating /k8s-for-beginners: reflect.rtype.IsVariadic: symbol not found
256Error relocating /k8s-for-beginners: reflect.rtype.IsVariadic: symbol not found
257Error relocating /k8s-for-beginners: reflect.rtype.Key: symbol not found
258Error relocating /k8s-for-beginners: reflect.rtype.Key: symbol not found
259Error relocating /k8s-for-beginners: reflect.rtype.Kind: symbol not found
260Error relocating /k8s-for-beginners: reflect.rtype.Kind: symbol not found
261Error relocating /k8s-for-beginners: reflect.rtype.Len: symbol not found
262Error relocating /k8s-for-beginners: reflect.rtype.Len: symbol not found
263Error relocating /k8s-for-beginners: reflect.rtype.Method: symbol not found
264Error relocating /k8s-for-beginners: reflect.rtype.Method: symbol not found
265Error relocating /k8s-for-beginners: reflect.rtype.MethodByName: symbol not found
266Error relocating /k8s-for-beginners: reflect.rtype.MethodByName: symbol not found
267Error relocating /k8s-for-beginners: reflect.rtype.Name: symbol not found
268Error relocating /k8s-for-beginners: reflect.rtype.Name: symbol not found
269Error relocating /k8s-for-beginners: reflect.rtype.NumField: symbol not found
270Error relocating /k8s-for-beginners: reflect.rtype.NumField: symbol not found
271Error relocating /k8s-for-beginners: reflect.rtype.NumIn: symbol not found
272Error relocating /k8s-for-beginners: reflect.rtype.NumIn: symbol not found
273Error relocating /k8s-for-beginners: reflect.rtype.NumMethod: symbol not found
274Error relocating /k8s-for-beginners: reflect.rtype.NumMethod: symbol not found
275Error relocating /k8s-for-beginners: reflect.rtype.NumOut: symbol not found
276Error relocating /k8s-for-beginners: reflect.rtype.NumOut: symbol not found
277Error relocating /k8s-for-beginners: reflect.rtype.Out: symbol not found
278Error relocating /k8s-for-beginners: reflect.rtype.Out: symbol not found
279Error relocating /k8s-for-beginners: reflect.rtype.PkgPath: symbol not found
280Error relocating /k8s-for-beginners: reflect.rtype.PkgPath: symbol not found
281Error relocating /k8s-for-beginners: reflect.rtype.Size: symbol not found
282Error relocating /k8s-for-beginners: reflect.rtype.Size: symbol not found
283Error relocating /k8s-for-beginners: reflect.rtype.String: symbol not found
284Error relocating /k8s-for-beginners: reflect.rtype.String: symbol not found
285Error relocating /k8s-for-beginners: reflect.rtype.common: symbol not found
286Error relocating /k8s-for-beginners: reflect.rtype.common: symbol not found
287Error relocating /k8s-for-beginners: reflect.rtype.rawString: symbol not found
288Error relocating /k8s-for-beginners: reflect.rtype.rawString: symbol not found
289Error relocating /k8s-for-beginners: reflect.rtype.uncommon..stub: symbol not found
290Error relocating /k8s-for-beginners: reflect.rtype.uncommon..stub: symbol not found
291Error relocating /k8s-for-beginners: reflect..reflect.rtype..d: symbol not found
292Error relocating /k8s-for-beginners: type...1net.IPAddr: symbol not found
293Error relocating /k8s-for-beginners: net.IPAddr.Network: symbol not found
294Error relocating /k8s-for-beginners: net.IPAddr.Network: symbol not found
295Error relocating /k8s-for-beginners: net.IPAddr.String: symbol not found
296Error relocating /k8s-for-beginners: net.IPAddr.String: symbol not found
297Error relocating /k8s-for-beginners: net.IPAddr.family: symbol not found
298Error relocating /k8s-for-beginners: net.IPAddr.family: symbol not found
299Error relocating /k8s-for-beginners: net.IPAddr.isWildcard: symbol not found
300Error relocating /k8s-for-beginners: net.IPAddr.isWildcard: symbol not found
301Error relocating /k8s-for-beginners: net.IPAddr.sockaddr: symbol not found
302Error relocating /k8s-for-beginners: net.IPAddr.sockaddr: symbol not found
303Error relocating /k8s-for-beginners: net.IPAddr.toLocal: symbol not found
304Error relocating /k8s-for-beginners: net.IPAddr.toLocal: symbol not found
305Error relocating /k8s-for-beginners: net.IPAddr..d: symbol not found
306Error relocating /k8s-for-beginners: runtime.main: symbol not found
307Error relocating /k8s-for-beginners: runtime_iscgo: symbol not found
308Error relocating /k8s-for-beginners: runtime_isstarted: symbol not found
309Error relocating /k8s-for-beginners: runtime_isarchive: symbol not found
310Error relocating /k8s-for-beginners: __gcc_personality_v0: symbol not found
311Error relocating /k8s-for-beginners: io.Writer..d: symbol not found
312Error relocating /k8s-for-beginners: runtime.writeBarrier: symbol not found
313
314

ANSWER

Answered 2021-Aug-22 at 15:41

Since you're already using Docker, I'd suggest using a multi-stage build. Using a standard docker image like golang one can build an executable asset which is guaranteed to work with other docker linux images:

1CONTAINER ID   IMAGE                      COMMAND                CREATED          STATUS                      PORTS     NAMES
2ae327a2bdba3   k8s-for-beginners:v0.0.1   &quot;/k8s-for-beginners&quot;   11 seconds ago   Exited (1) 10 seconds ago             focused_booth
3standard_init_linux.go:228: exec user process caused: no such file or directory
4FROM alpine:3.10
5COPY k8s-for-beginners /
6CMD [&quot;/k8s-for-beginners&quot;]
7package main
8
9import (
10    &quot;fmt&quot;
11    &quot;log&quot;
12    &quot;net/http&quot;
13)
14
15func main() {
16    http.HandleFunc(&quot;/&quot;, handler)
17    log.Fatal(http.ListenAndServe(&quot;0.0.0.0:8080&quot;, nil))
18}
19
20func handler(w http.ResponseWriter, r *http.Request) {
21    log.Printf(&quot;Ping from %s&quot;, r.RemoteAddr)
22    fmt.Fprintln(w, &quot;Hello Kubernetes Beginners!&quot;)
23}
24CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o k8s-for-beginners
25sudo docker build -t k8s-for-beginners:v0.0.1 .
26sudo docker run -p 8080:8080 -d k8s-for-beginners:v0.0.1
27        /lib64/ld-linux-x86-64.so.2 (0x7f9ab5778000)
28        libc.so.6 =&gt; /lib64/ld-linux-x86-64.so.2 (0x7f9ab5778000)
29Error loading shared library libgo.so.16: No such file or directory (needed by /k8s-for-beginners)
30Error loading shared library libgcc_s.so.1: No such file or directory (needed by /k8s-for-beginners)
31Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by /k8s-for-beginners)
32Error relocating /k8s-for-beginners: crypto..z2frsa..import: symbol not found
33Error relocating /k8s-for-beginners: crypto..z2fx509..import: symbol not found
34Error relocating /k8s-for-beginners: log..import: symbol not found
35Error relocating /k8s-for-beginners: crypto..z2fmd5..import: symbol not found
36Error relocating /k8s-for-beginners: crypto..import: symbol not found
37Error relocating /k8s-for-beginners: bytes..import: symbol not found
38Error relocating /k8s-for-beginners: fmt.Fprintln: symbol not found
39Error relocating /k8s-for-beginners: crypto..z2felliptic..import: symbol not found
40Error relocating /k8s-for-beginners: crypto..z2fx509..z2fpkix..import: symbol not found
41Error relocating /k8s-for-beginners: crypto..z2frand..import: symbol not found
42Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fchacha20poly1305..import: symbol not found
43Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcurve25519..import: symbol not found
44Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fidna..import: symbol not found
45Error relocating /k8s-for-beginners: internal..z2foserror..import: symbol not found
46Error relocating /k8s-for-beginners: crypto..z2fecdsa..import: symbol not found
47Error relocating /k8s-for-beginners: net..z2fhttp.HandleFunc: symbol not found
48Error relocating /k8s-for-beginners: io..import: symbol not found
49Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp2..z2fhpack..import: symbol not found
50Error relocating /k8s-for-beginners: crypto..z2fcipher..import: symbol not found
51Error relocating /k8s-for-beginners: log.Fatal: symbol not found
52Error relocating /k8s-for-beginners: math..z2fbig..import: symbol not found
53Error relocating /k8s-for-beginners: runtime..import: symbol not found
54Error relocating /k8s-for-beginners: net..z2fhttp..import: symbol not found
55Error relocating /k8s-for-beginners: hash..z2fcrc32..import: symbol not found
56Error relocating /k8s-for-beginners: net..z2fhttp.ListenAndServe: symbol not found
57Error relocating /k8s-for-beginners: context..import: symbol not found
58Error relocating /k8s-for-beginners: fmt..import: symbol not found
59Error relocating /k8s-for-beginners: crypto..z2ftls..import: symbol not found
60Error relocating /k8s-for-beginners: errors..import: symbol not found
61Error relocating /k8s-for-beginners: internal..z2ftestlog..import: symbol not found
62Error relocating /k8s-for-beginners: runtime.setIsCgo: symbol not found
63Error relocating /k8s-for-beginners: runtime_m: symbol not found
64Error relocating /k8s-for-beginners: encoding..z2fhex..import: symbol not found
65Error relocating /k8s-for-beginners: mime..import: symbol not found
66Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2funicode..z2fbidi..import: symbol not found
67Error relocating /k8s-for-beginners: internal..z2freflectlite..import: symbol not found
68Error relocating /k8s-for-beginners: compress..z2fgzip..import: symbol not found
69Error relocating /k8s-for-beginners: sync..import: symbol not found
70Error relocating /k8s-for-beginners: compress..z2fflate..import: symbol not found
71Error relocating /k8s-for-beginners: encoding..z2fbinary..import: symbol not found
72Error relocating /k8s-for-beginners: math..z2frand..import: symbol not found
73Error relocating /k8s-for-beginners: runtime_cpuinit: symbol not found
74Error relocating /k8s-for-beginners: internal..z2fpoll..import: symbol not found
75Error relocating /k8s-for-beginners: mime..z2fmultipart..import: symbol not found
76Error relocating /k8s-for-beginners: runtime.check: symbol not found
77Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcryptobyte..import: symbol not found
78Error relocating /k8s-for-beginners: crypto..z2fsha512..import: symbol not found
79Error relocating /k8s-for-beginners: runtime.registerTypeDescriptors: symbol not found
80Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fchacha20..import: symbol not found
81Error relocating /k8s-for-beginners: runtime.setmodinfo: symbol not found
82Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2ftransform..import: symbol not found
83Error relocating /k8s-for-beginners: time..import: symbol not found
84Error relocating /k8s-for-beginners: encoding..z2fbase64..import: symbol not found
85Error relocating /k8s-for-beginners: crypto..z2fsha256..import: symbol not found
86Error relocating /k8s-for-beginners: __go_go: symbol not found
87Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp..z2fhttpguts..import: symbol not found
88Error relocating /k8s-for-beginners: path..z2ffilepath..import: symbol not found
89Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2fsecure..z2fbidirule..import: symbol not found
90Error relocating /k8s-for-beginners: os..import: symbol not found
91Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp..z2fhttpproxy..import: symbol not found
92Error relocating /k8s-for-beginners: net..z2ftextproto..import: symbol not found
93Error relocating /k8s-for-beginners: encoding..z2fasn1..import: symbol not found
94Error relocating /k8s-for-beginners: runtime.requireitab: symbol not found
95Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fdns..z2fdnsmessage..import: symbol not found
96Error relocating /k8s-for-beginners: path..import: symbol not found
97Error relocating /k8s-for-beginners: io..z2fioutil..import: symbol not found
98Error relocating /k8s-for-beginners: sort..import: symbol not found
99Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2funicode..z2fnorm..import: symbol not found
100Error relocating /k8s-for-beginners: internal..z2fcpu..import: symbol not found
101Error relocating /k8s-for-beginners: runtime.ginit: symbol not found
102Error relocating /k8s-for-beginners: runtime.osinit: symbol not found
103Error relocating /k8s-for-beginners: runtime.schedinit: symbol not found
104Error relocating /k8s-for-beginners: bufio..import: symbol not found
105Error relocating /k8s-for-beginners: crypto..z2finternal..z2frandutil..import: symbol not found
106Error relocating /k8s-for-beginners: runtime_mstart: symbol not found
107Error relocating /k8s-for-beginners: net..import: symbol not found
108Error relocating /k8s-for-beginners: strconv..import: symbol not found
109Error relocating /k8s-for-beginners: runtime.args: symbol not found
110Error relocating /k8s-for-beginners: runtime..z2finternal..z2fsys..import: symbol not found
111Error relocating /k8s-for-beginners: runtime.newobject: symbol not found
112Error relocating /k8s-for-beginners: syscall..import: symbol not found
113Error relocating /k8s-for-beginners: unicode..import: symbol not found
114Error relocating /k8s-for-beginners: net..z2fhttp..z2finternal..import: symbol not found
115Error relocating /k8s-for-beginners: encoding..z2fpem..import: symbol not found
116Error relocating /k8s-for-beginners: _Unwind_Resume: symbol not found
117Error relocating /k8s-for-beginners: reflect..import: symbol not found
118Error relocating /k8s-for-beginners: mime..z2fquotedprintable..import: symbol not found
119Error relocating /k8s-for-beginners: log.Printf: symbol not found
120Error relocating /k8s-for-beginners: runtime.typedmemmove: symbol not found
121Error relocating /k8s-for-beginners: crypto..z2fdsa..import: symbol not found
122Error relocating /k8s-for-beginners: crypto..z2fsha1..import: symbol not found
123Error relocating /k8s-for-beginners: bufio..types: symbol not found
124Error relocating /k8s-for-beginners: bytes..types: symbol not found
125Error relocating /k8s-for-beginners: compress..z2fflate..types: symbol not found
126Error relocating /k8s-for-beginners: compress..z2fgzip..types: symbol not found
127Error relocating /k8s-for-beginners: context..types: symbol not found
128Error relocating /k8s-for-beginners: crypto..types: symbol not found
129Error relocating /k8s-for-beginners: crypto..z2fcipher..types: symbol not found
130Error relocating /k8s-for-beginners: crypto..z2fdsa..types: symbol not found
131Error relocating /k8s-for-beginners: crypto..z2fecdsa..types: symbol not found
132Error relocating /k8s-for-beginners: crypto..z2felliptic..types: symbol not found
133Error relocating /k8s-for-beginners: crypto..z2finternal..z2frandutil..types: symbol not found
134Error relocating /k8s-for-beginners: crypto..z2fmd5..types: symbol not found
135Error relocating /k8s-for-beginners: crypto..z2frand..types: symbol not found
136Error relocating /k8s-for-beginners: crypto..z2frsa..types: symbol not found
137Error relocating /k8s-for-beginners: crypto..z2fsha1..types: symbol not found
138Error relocating /k8s-for-beginners: crypto..z2fsha256..types: symbol not found
139Error relocating /k8s-for-beginners: crypto..z2fsha512..types: symbol not found
140Error relocating /k8s-for-beginners: crypto..z2ftls..types: symbol not found
141Error relocating /k8s-for-beginners: crypto..z2fx509..types: symbol not found
142Error relocating /k8s-for-beginners: crypto..z2fx509..z2fpkix..types: symbol not found
143Error relocating /k8s-for-beginners: encoding..z2fasn1..types: symbol not found
144Error relocating /k8s-for-beginners: encoding..z2fbase64..types: symbol not found
145Error relocating /k8s-for-beginners: encoding..z2fbinary..types: symbol not found
146Error relocating /k8s-for-beginners: encoding..z2fhex..types: symbol not found
147Error relocating /k8s-for-beginners: encoding..z2fpem..types: symbol not found
148Error relocating /k8s-for-beginners: errors..types: symbol not found
149Error relocating /k8s-for-beginners: fmt..types: symbol not found
150Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fchacha20..types: symbol not found
151Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fchacha20poly1305..types: symbol not found
152Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcryptobyte..types: symbol not found
153Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcurve25519..types: symbol not found
154Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fdns..z2fdnsmessage..types: symbol not found
155Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp..z2fhttpguts..types: symbol not found
156Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp..z2fhttpproxy..types: symbol not found
157Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp2..z2fhpack..types: symbol not found
158Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fidna..types: symbol not found
159Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2fsecure..z2fbidirule..types: symbol not found
160Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2ftransform..types: symbol not found
161Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2funicode..z2fbidi..types: symbol not found
162Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2funicode..z2fnorm..types: symbol not found
163Error relocating /k8s-for-beginners: hash..z2fcrc32..types: symbol not found
164Error relocating /k8s-for-beginners: internal..z2fcpu..types: symbol not found
165Error relocating /k8s-for-beginners: internal..z2foserror..types: symbol not found
166Error relocating /k8s-for-beginners: internal..z2fpoll..types: symbol not found
167Error relocating /k8s-for-beginners: internal..z2freflectlite..types: symbol not found
168Error relocating /k8s-for-beginners: internal..z2ftestlog..types: symbol not found
169Error relocating /k8s-for-beginners: io..types: symbol not found
170Error relocating /k8s-for-beginners: io..z2fioutil..types: symbol not found
171Error relocating /k8s-for-beginners: log..types: symbol not found
172Error relocating /k8s-for-beginners: math..z2fbig..types: symbol not found
173Error relocating /k8s-for-beginners: math..z2frand..types: symbol not found
174Error relocating /k8s-for-beginners: mime..types: symbol not found
175Error relocating /k8s-for-beginners: mime..z2fmultipart..types: symbol not found
176Error relocating /k8s-for-beginners: mime..z2fquotedprintable..types: symbol not found
177Error relocating /k8s-for-beginners: net..types: symbol not found
178Error relocating /k8s-for-beginners: net..z2fhttp..types: symbol not found
179Error relocating /k8s-for-beginners: net..z2fhttp..z2finternal..types: symbol not found
180Error relocating /k8s-for-beginners: net..z2ftextproto..types: symbol not found
181Error relocating /k8s-for-beginners: os..types: symbol not found
182Error relocating /k8s-for-beginners: path..types: symbol not found
183Error relocating /k8s-for-beginners: path..z2ffilepath..types: symbol not found
184Error relocating /k8s-for-beginners: reflect..types: symbol not found
185Error relocating /k8s-for-beginners: runtime..types: symbol not found
186Error relocating /k8s-for-beginners: runtime..z2finternal..z2fsys..types: symbol not found
187Error relocating /k8s-for-beginners: sort..types: symbol not found
188Error relocating /k8s-for-beginners: strconv..types: symbol not found
189Error relocating /k8s-for-beginners: sync..types: symbol not found
190Error relocating /k8s-for-beginners: syscall..types: symbol not found
191Error relocating /k8s-for-beginners: time..types: symbol not found
192Error relocating /k8s-for-beginners: unicode..types: symbol not found
193Error relocating /k8s-for-beginners: container..z2flist..types: symbol not found
194Error relocating /k8s-for-beginners: crypto..z2faes..types: symbol not found
195Error relocating /k8s-for-beginners: crypto..z2fdes..types: symbol not found
196Error relocating /k8s-for-beginners: crypto..z2fed25519..types: symbol not found
197Error relocating /k8s-for-beginners: crypto..z2fed25519..z2finternal..z2fedwards25519..types: symbol not found
198Error relocating /k8s-for-beginners: crypto..z2fhmac..types: symbol not found
199Error relocating /k8s-for-beginners: crypto..z2finternal..z2fsubtle..types: symbol not found
200Error relocating /k8s-for-beginners: crypto..z2frc4..types: symbol not found
201Error relocating /k8s-for-beginners: crypto..z2fsubtle..types: symbol not found
202Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcryptobyte..z2fasn1..types: symbol not found
203Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fhkdf..types: symbol not found
204Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2finternal..z2fsubtle..types: symbol not found
205Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fpoly1305..types: symbol not found
206Error relocating /k8s-for-beginners: hash..types: symbol not found
207Error relocating /k8s-for-beginners: internal..z2fbytealg..types: symbol not found
208Error relocating /k8s-for-beginners: internal..z2ffmtsort..types: symbol not found
209Error relocating /k8s-for-beginners: internal..z2fnettrace..types: symbol not found
210Error relocating /k8s-for-beginners: internal..z2frace..types: symbol not found
211Error relocating /k8s-for-beginners: internal..z2fsingleflight..types: symbol not found
212Error relocating /k8s-for-beginners: internal..z2fsyscall..z2fexecenv..types: symbol not found
213Error relocating /k8s-for-beginners: internal..z2fsyscall..z2funix..types: symbol not found
214Error relocating /k8s-for-beginners: math..types: symbol not found
215Error relocating /k8s-for-beginners: math..z2fbits..types: symbol not found
216Error relocating /k8s-for-beginners: net..z2fhttp..z2fhttptrace..types: symbol not found
217Error relocating /k8s-for-beginners: net..z2furl..types: symbol not found
218Error relocating /k8s-for-beginners: runtime..z2finternal..z2fatomic..types: symbol not found
219Error relocating /k8s-for-beginners: runtime..z2finternal..z2fmath..types: symbol not found
220Error relocating /k8s-for-beginners: strings..types: symbol not found
221Error relocating /k8s-for-beginners: sync..z2fatomic..types: symbol not found
222Error relocating /k8s-for-beginners: unicode..z2futf16..types: symbol not found
223Error relocating /k8s-for-beginners: unicode..z2futf8..types: symbol not found
224Error relocating /k8s-for-beginners: runtime.strequal..f: symbol not found
225Error relocating /k8s-for-beginners: runtime.memequal64..f: symbol not found
226Error relocating /k8s-for-beginners: type...1reflect.rtype: symbol not found
227Error relocating /k8s-for-beginners: reflect.rtype.Align: symbol not found
228Error relocating /k8s-for-beginners: reflect.rtype.Align: symbol not found
229Error relocating /k8s-for-beginners: reflect.rtype.AssignableTo: symbol not found
230Error relocating /k8s-for-beginners: reflect.rtype.AssignableTo: symbol not found
231Error relocating /k8s-for-beginners: reflect.rtype.Bits: symbol not found
232Error relocating /k8s-for-beginners: reflect.rtype.Bits: symbol not found
233Error relocating /k8s-for-beginners: reflect.rtype.ChanDir: symbol not found
234Error relocating /k8s-for-beginners: reflect.rtype.ChanDir: symbol not found
235Error relocating /k8s-for-beginners: reflect.rtype.Comparable: symbol not found
236Error relocating /k8s-for-beginners: reflect.rtype.Comparable: symbol not found
237Error relocating /k8s-for-beginners: reflect.rtype.ConvertibleTo: symbol not found
238Error relocating /k8s-for-beginners: reflect.rtype.ConvertibleTo: symbol not found
239Error relocating /k8s-for-beginners: reflect.rtype.Elem: symbol not found
240Error relocating /k8s-for-beginners: reflect.rtype.Elem: symbol not found
241Error relocating /k8s-for-beginners: reflect.rtype.Field: symbol not found
242Error relocating /k8s-for-beginners: reflect.rtype.Field: symbol not found
243Error relocating /k8s-for-beginners: reflect.rtype.FieldAlign: symbol not found
244Error relocating /k8s-for-beginners: reflect.rtype.FieldAlign: symbol not found
245Error relocating /k8s-for-beginners: reflect.rtype.FieldByIndex: symbol not found
246Error relocating /k8s-for-beginners: reflect.rtype.FieldByIndex: symbol not found
247Error relocating /k8s-for-beginners: reflect.rtype.FieldByName: symbol not found
248Error relocating /k8s-for-beginners: reflect.rtype.FieldByName: symbol not found
249Error relocating /k8s-for-beginners: reflect.rtype.FieldByNameFunc: symbol not found
250Error relocating /k8s-for-beginners: reflect.rtype.FieldByNameFunc: symbol not found
251Error relocating /k8s-for-beginners: reflect.rtype.Implements: symbol not found
252Error relocating /k8s-for-beginners: reflect.rtype.Implements: symbol not found
253Error relocating /k8s-for-beginners: reflect.rtype.In: symbol not found
254Error relocating /k8s-for-beginners: reflect.rtype.In: symbol not found
255Error relocating /k8s-for-beginners: reflect.rtype.IsVariadic: symbol not found
256Error relocating /k8s-for-beginners: reflect.rtype.IsVariadic: symbol not found
257Error relocating /k8s-for-beginners: reflect.rtype.Key: symbol not found
258Error relocating /k8s-for-beginners: reflect.rtype.Key: symbol not found
259Error relocating /k8s-for-beginners: reflect.rtype.Kind: symbol not found
260Error relocating /k8s-for-beginners: reflect.rtype.Kind: symbol not found
261Error relocating /k8s-for-beginners: reflect.rtype.Len: symbol not found
262Error relocating /k8s-for-beginners: reflect.rtype.Len: symbol not found
263Error relocating /k8s-for-beginners: reflect.rtype.Method: symbol not found
264Error relocating /k8s-for-beginners: reflect.rtype.Method: symbol not found
265Error relocating /k8s-for-beginners: reflect.rtype.MethodByName: symbol not found
266Error relocating /k8s-for-beginners: reflect.rtype.MethodByName: symbol not found
267Error relocating /k8s-for-beginners: reflect.rtype.Name: symbol not found
268Error relocating /k8s-for-beginners: reflect.rtype.Name: symbol not found
269Error relocating /k8s-for-beginners: reflect.rtype.NumField: symbol not found
270Error relocating /k8s-for-beginners: reflect.rtype.NumField: symbol not found
271Error relocating /k8s-for-beginners: reflect.rtype.NumIn: symbol not found
272Error relocating /k8s-for-beginners: reflect.rtype.NumIn: symbol not found
273Error relocating /k8s-for-beginners: reflect.rtype.NumMethod: symbol not found
274Error relocating /k8s-for-beginners: reflect.rtype.NumMethod: symbol not found
275Error relocating /k8s-for-beginners: reflect.rtype.NumOut: symbol not found
276Error relocating /k8s-for-beginners: reflect.rtype.NumOut: symbol not found
277Error relocating /k8s-for-beginners: reflect.rtype.Out: symbol not found
278Error relocating /k8s-for-beginners: reflect.rtype.Out: symbol not found
279Error relocating /k8s-for-beginners: reflect.rtype.PkgPath: symbol not found
280Error relocating /k8s-for-beginners: reflect.rtype.PkgPath: symbol not found
281Error relocating /k8s-for-beginners: reflect.rtype.Size: symbol not found
282Error relocating /k8s-for-beginners: reflect.rtype.Size: symbol not found
283Error relocating /k8s-for-beginners: reflect.rtype.String: symbol not found
284Error relocating /k8s-for-beginners: reflect.rtype.String: symbol not found
285Error relocating /k8s-for-beginners: reflect.rtype.common: symbol not found
286Error relocating /k8s-for-beginners: reflect.rtype.common: symbol not found
287Error relocating /k8s-for-beginners: reflect.rtype.rawString: symbol not found
288Error relocating /k8s-for-beginners: reflect.rtype.rawString: symbol not found
289Error relocating /k8s-for-beginners: reflect.rtype.uncommon..stub: symbol not found
290Error relocating /k8s-for-beginners: reflect.rtype.uncommon..stub: symbol not found
291Error relocating /k8s-for-beginners: reflect..reflect.rtype..d: symbol not found
292Error relocating /k8s-for-beginners: type...1net.IPAddr: symbol not found
293Error relocating /k8s-for-beginners: net.IPAddr.Network: symbol not found
294Error relocating /k8s-for-beginners: net.IPAddr.Network: symbol not found
295Error relocating /k8s-for-beginners: net.IPAddr.String: symbol not found
296Error relocating /k8s-for-beginners: net.IPAddr.String: symbol not found
297Error relocating /k8s-for-beginners: net.IPAddr.family: symbol not found
298Error relocating /k8s-for-beginners: net.IPAddr.family: symbol not found
299Error relocating /k8s-for-beginners: net.IPAddr.isWildcard: symbol not found
300Error relocating /k8s-for-beginners: net.IPAddr.isWildcard: symbol not found
301Error relocating /k8s-for-beginners: net.IPAddr.sockaddr: symbol not found
302Error relocating /k8s-for-beginners: net.IPAddr.sockaddr: symbol not found
303Error relocating /k8s-for-beginners: net.IPAddr.toLocal: symbol not found
304Error relocating /k8s-for-beginners: net.IPAddr.toLocal: symbol not found
305Error relocating /k8s-for-beginners: net.IPAddr..d: symbol not found
306Error relocating /k8s-for-beginners: runtime.main: symbol not found
307Error relocating /k8s-for-beginners: runtime_iscgo: symbol not found
308Error relocating /k8s-for-beginners: runtime_isstarted: symbol not found
309Error relocating /k8s-for-beginners: runtime_isarchive: symbol not found
310Error relocating /k8s-for-beginners: __gcc_personality_v0: symbol not found
311Error relocating /k8s-for-beginners: io.Writer..d: symbol not found
312Error relocating /k8s-for-beginners: runtime.writeBarrier: symbol not found
313
314FROM golang:1.17 as builder
315
316# first (build) stage
317
318WORKDIR /app
319COPY . .
320RUN go mod download
321RUN CGO_ENABLED=0 go build -o k8s-for-beginners
322
323# final (target) stage
324
325FROM alpine:3.10
326COPY --from=builder /app/k8s-for-beginners /
327CMD [&quot;/k8s-for-beginners&quot;]
328

Note: you since you are using CGO_ENABLED=0 you can run your app in a much smaller scratch container. To do so replace FROM alpine:3.10 with FROM scratch


P.S. ensure you have a go.mod file in the same directory as the Go source and Dockerfile:

1CONTAINER ID   IMAGE                      COMMAND                CREATED          STATUS                      PORTS     NAMES
2ae327a2bdba3   k8s-for-beginners:v0.0.1   &quot;/k8s-for-beginners&quot;   11 seconds ago   Exited (1) 10 seconds ago             focused_booth
3standard_init_linux.go:228: exec user process caused: no such file or directory
4FROM alpine:3.10
5COPY k8s-for-beginners /
6CMD [&quot;/k8s-for-beginners&quot;]
7package main
8
9import (
10    &quot;fmt&quot;
11    &quot;log&quot;
12    &quot;net/http&quot;
13)
14
15func main() {
16    http.HandleFunc(&quot;/&quot;, handler)
17    log.Fatal(http.ListenAndServe(&quot;0.0.0.0:8080&quot;, nil))
18}
19
20func handler(w http.ResponseWriter, r *http.Request) {
21    log.Printf(&quot;Ping from %s&quot;, r.RemoteAddr)
22    fmt.Fprintln(w, &quot;Hello Kubernetes Beginners!&quot;)
23}
24CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o k8s-for-beginners
25sudo docker build -t k8s-for-beginners:v0.0.1 .
26sudo docker run -p 8080:8080 -d k8s-for-beginners:v0.0.1
27        /lib64/ld-linux-x86-64.so.2 (0x7f9ab5778000)
28        libc.so.6 =&gt; /lib64/ld-linux-x86-64.so.2 (0x7f9ab5778000)
29Error loading shared library libgo.so.16: No such file or directory (needed by /k8s-for-beginners)
30Error loading shared library libgcc_s.so.1: No such file or directory (needed by /k8s-for-beginners)
31Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by /k8s-for-beginners)
32Error relocating /k8s-for-beginners: crypto..z2frsa..import: symbol not found
33Error relocating /k8s-for-beginners: crypto..z2fx509..import: symbol not found
34Error relocating /k8s-for-beginners: log..import: symbol not found
35Error relocating /k8s-for-beginners: crypto..z2fmd5..import: symbol not found
36Error relocating /k8s-for-beginners: crypto..import: symbol not found
37Error relocating /k8s-for-beginners: bytes..import: symbol not found
38Error relocating /k8s-for-beginners: fmt.Fprintln: symbol not found
39Error relocating /k8s-for-beginners: crypto..z2felliptic..import: symbol not found
40Error relocating /k8s-for-beginners: crypto..z2fx509..z2fpkix..import: symbol not found
41Error relocating /k8s-for-beginners: crypto..z2frand..import: symbol not found
42Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fchacha20poly1305..import: symbol not found
43Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcurve25519..import: symbol not found
44Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fidna..import: symbol not found
45Error relocating /k8s-for-beginners: internal..z2foserror..import: symbol not found
46Error relocating /k8s-for-beginners: crypto..z2fecdsa..import: symbol not found
47Error relocating /k8s-for-beginners: net..z2fhttp.HandleFunc: symbol not found
48Error relocating /k8s-for-beginners: io..import: symbol not found
49Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp2..z2fhpack..import: symbol not found
50Error relocating /k8s-for-beginners: crypto..z2fcipher..import: symbol not found
51Error relocating /k8s-for-beginners: log.Fatal: symbol not found
52Error relocating /k8s-for-beginners: math..z2fbig..import: symbol not found
53Error relocating /k8s-for-beginners: runtime..import: symbol not found
54Error relocating /k8s-for-beginners: net..z2fhttp..import: symbol not found
55Error relocating /k8s-for-beginners: hash..z2fcrc32..import: symbol not found
56Error relocating /k8s-for-beginners: net..z2fhttp.ListenAndServe: symbol not found
57Error relocating /k8s-for-beginners: context..import: symbol not found
58Error relocating /k8s-for-beginners: fmt..import: symbol not found
59Error relocating /k8s-for-beginners: crypto..z2ftls..import: symbol not found
60Error relocating /k8s-for-beginners: errors..import: symbol not found
61Error relocating /k8s-for-beginners: internal..z2ftestlog..import: symbol not found
62Error relocating /k8s-for-beginners: runtime.setIsCgo: symbol not found
63Error relocating /k8s-for-beginners: runtime_m: symbol not found
64Error relocating /k8s-for-beginners: encoding..z2fhex..import: symbol not found
65Error relocating /k8s-for-beginners: mime..import: symbol not found
66Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2funicode..z2fbidi..import: symbol not found
67Error relocating /k8s-for-beginners: internal..z2freflectlite..import: symbol not found
68Error relocating /k8s-for-beginners: compress..z2fgzip..import: symbol not found
69Error relocating /k8s-for-beginners: sync..import: symbol not found
70Error relocating /k8s-for-beginners: compress..z2fflate..import: symbol not found
71Error relocating /k8s-for-beginners: encoding..z2fbinary..import: symbol not found
72Error relocating /k8s-for-beginners: math..z2frand..import: symbol not found
73Error relocating /k8s-for-beginners: runtime_cpuinit: symbol not found
74Error relocating /k8s-for-beginners: internal..z2fpoll..import: symbol not found
75Error relocating /k8s-for-beginners: mime..z2fmultipart..import: symbol not found
76Error relocating /k8s-for-beginners: runtime.check: symbol not found
77Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcryptobyte..import: symbol not found
78Error relocating /k8s-for-beginners: crypto..z2fsha512..import: symbol not found
79Error relocating /k8s-for-beginners: runtime.registerTypeDescriptors: symbol not found
80Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fchacha20..import: symbol not found
81Error relocating /k8s-for-beginners: runtime.setmodinfo: symbol not found
82Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2ftransform..import: symbol not found
83Error relocating /k8s-for-beginners: time..import: symbol not found
84Error relocating /k8s-for-beginners: encoding..z2fbase64..import: symbol not found
85Error relocating /k8s-for-beginners: crypto..z2fsha256..import: symbol not found
86Error relocating /k8s-for-beginners: __go_go: symbol not found
87Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp..z2fhttpguts..import: symbol not found
88Error relocating /k8s-for-beginners: path..z2ffilepath..import: symbol not found
89Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2fsecure..z2fbidirule..import: symbol not found
90Error relocating /k8s-for-beginners: os..import: symbol not found
91Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp..z2fhttpproxy..import: symbol not found
92Error relocating /k8s-for-beginners: net..z2ftextproto..import: symbol not found
93Error relocating /k8s-for-beginners: encoding..z2fasn1..import: symbol not found
94Error relocating /k8s-for-beginners: runtime.requireitab: symbol not found
95Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fdns..z2fdnsmessage..import: symbol not found
96Error relocating /k8s-for-beginners: path..import: symbol not found
97Error relocating /k8s-for-beginners: io..z2fioutil..import: symbol not found
98Error relocating /k8s-for-beginners: sort..import: symbol not found
99Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2funicode..z2fnorm..import: symbol not found
100Error relocating /k8s-for-beginners: internal..z2fcpu..import: symbol not found
101Error relocating /k8s-for-beginners: runtime.ginit: symbol not found
102Error relocating /k8s-for-beginners: runtime.osinit: symbol not found
103Error relocating /k8s-for-beginners: runtime.schedinit: symbol not found
104Error relocating /k8s-for-beginners: bufio..import: symbol not found
105Error relocating /k8s-for-beginners: crypto..z2finternal..z2frandutil..import: symbol not found
106Error relocating /k8s-for-beginners: runtime_mstart: symbol not found
107Error relocating /k8s-for-beginners: net..import: symbol not found
108Error relocating /k8s-for-beginners: strconv..import: symbol not found
109Error relocating /k8s-for-beginners: runtime.args: symbol not found
110Error relocating /k8s-for-beginners: runtime..z2finternal..z2fsys..import: symbol not found
111Error relocating /k8s-for-beginners: runtime.newobject: symbol not found
112Error relocating /k8s-for-beginners: syscall..import: symbol not found
113Error relocating /k8s-for-beginners: unicode..import: symbol not found
114Error relocating /k8s-for-beginners: net..z2fhttp..z2finternal..import: symbol not found
115Error relocating /k8s-for-beginners: encoding..z2fpem..import: symbol not found
116Error relocating /k8s-for-beginners: _Unwind_Resume: symbol not found
117Error relocating /k8s-for-beginners: reflect..import: symbol not found
118Error relocating /k8s-for-beginners: mime..z2fquotedprintable..import: symbol not found
119Error relocating /k8s-for-beginners: log.Printf: symbol not found
120Error relocating /k8s-for-beginners: runtime.typedmemmove: symbol not found
121Error relocating /k8s-for-beginners: crypto..z2fdsa..import: symbol not found
122Error relocating /k8s-for-beginners: crypto..z2fsha1..import: symbol not found
123Error relocating /k8s-for-beginners: bufio..types: symbol not found
124Error relocating /k8s-for-beginners: bytes..types: symbol not found
125Error relocating /k8s-for-beginners: compress..z2fflate..types: symbol not found
126Error relocating /k8s-for-beginners: compress..z2fgzip..types: symbol not found
127Error relocating /k8s-for-beginners: context..types: symbol not found
128Error relocating /k8s-for-beginners: crypto..types: symbol not found
129Error relocating /k8s-for-beginners: crypto..z2fcipher..types: symbol not found
130Error relocating /k8s-for-beginners: crypto..z2fdsa..types: symbol not found
131Error relocating /k8s-for-beginners: crypto..z2fecdsa..types: symbol not found
132Error relocating /k8s-for-beginners: crypto..z2felliptic..types: symbol not found
133Error relocating /k8s-for-beginners: crypto..z2finternal..z2frandutil..types: symbol not found
134Error relocating /k8s-for-beginners: crypto..z2fmd5..types: symbol not found
135Error relocating /k8s-for-beginners: crypto..z2frand..types: symbol not found
136Error relocating /k8s-for-beginners: crypto..z2frsa..types: symbol not found
137Error relocating /k8s-for-beginners: crypto..z2fsha1..types: symbol not found
138Error relocating /k8s-for-beginners: crypto..z2fsha256..types: symbol not found
139Error relocating /k8s-for-beginners: crypto..z2fsha512..types: symbol not found
140Error relocating /k8s-for-beginners: crypto..z2ftls..types: symbol not found
141Error relocating /k8s-for-beginners: crypto..z2fx509..types: symbol not found
142Error relocating /k8s-for-beginners: crypto..z2fx509..z2fpkix..types: symbol not found
143Error relocating /k8s-for-beginners: encoding..z2fasn1..types: symbol not found
144Error relocating /k8s-for-beginners: encoding..z2fbase64..types: symbol not found
145Error relocating /k8s-for-beginners: encoding..z2fbinary..types: symbol not found
146Error relocating /k8s-for-beginners: encoding..z2fhex..types: symbol not found
147Error relocating /k8s-for-beginners: encoding..z2fpem..types: symbol not found
148Error relocating /k8s-for-beginners: errors..types: symbol not found
149Error relocating /k8s-for-beginners: fmt..types: symbol not found
150Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fchacha20..types: symbol not found
151Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fchacha20poly1305..types: symbol not found
152Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcryptobyte..types: symbol not found
153Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcurve25519..types: symbol not found
154Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fdns..z2fdnsmessage..types: symbol not found
155Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp..z2fhttpguts..types: symbol not found
156Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp..z2fhttpproxy..types: symbol not found
157Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp2..z2fhpack..types: symbol not found
158Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fidna..types: symbol not found
159Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2fsecure..z2fbidirule..types: symbol not found
160Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2ftransform..types: symbol not found
161Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2funicode..z2fbidi..types: symbol not found
162Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2funicode..z2fnorm..types: symbol not found
163Error relocating /k8s-for-beginners: hash..z2fcrc32..types: symbol not found
164Error relocating /k8s-for-beginners: internal..z2fcpu..types: symbol not found
165Error relocating /k8s-for-beginners: internal..z2foserror..types: symbol not found
166Error relocating /k8s-for-beginners: internal..z2fpoll..types: symbol not found
167Error relocating /k8s-for-beginners: internal..z2freflectlite..types: symbol not found
168Error relocating /k8s-for-beginners: internal..z2ftestlog..types: symbol not found
169Error relocating /k8s-for-beginners: io..types: symbol not found
170Error relocating /k8s-for-beginners: io..z2fioutil..types: symbol not found
171Error relocating /k8s-for-beginners: log..types: symbol not found
172Error relocating /k8s-for-beginners: math..z2fbig..types: symbol not found
173Error relocating /k8s-for-beginners: math..z2frand..types: symbol not found
174Error relocating /k8s-for-beginners: mime..types: symbol not found
175Error relocating /k8s-for-beginners: mime..z2fmultipart..types: symbol not found
176Error relocating /k8s-for-beginners: mime..z2fquotedprintable..types: symbol not found
177Error relocating /k8s-for-beginners: net..types: symbol not found
178Error relocating /k8s-for-beginners: net..z2fhttp..types: symbol not found
179Error relocating /k8s-for-beginners: net..z2fhttp..z2finternal..types: symbol not found
180Error relocating /k8s-for-beginners: net..z2ftextproto..types: symbol not found
181Error relocating /k8s-for-beginners: os..types: symbol not found
182Error relocating /k8s-for-beginners: path..types: symbol not found
183Error relocating /k8s-for-beginners: path..z2ffilepath..types: symbol not found
184Error relocating /k8s-for-beginners: reflect..types: symbol not found
185Error relocating /k8s-for-beginners: runtime..types: symbol not found
186Error relocating /k8s-for-beginners: runtime..z2finternal..z2fsys..types: symbol not found
187Error relocating /k8s-for-beginners: sort..types: symbol not found
188Error relocating /k8s-for-beginners: strconv..types: symbol not found
189Error relocating /k8s-for-beginners: sync..types: symbol not found
190Error relocating /k8s-for-beginners: syscall..types: symbol not found
191Error relocating /k8s-for-beginners: time..types: symbol not found
192Error relocating /k8s-for-beginners: unicode..types: symbol not found
193Error relocating /k8s-for-beginners: container..z2flist..types: symbol not found
194Error relocating /k8s-for-beginners: crypto..z2faes..types: symbol not found
195Error relocating /k8s-for-beginners: crypto..z2fdes..types: symbol not found
196Error relocating /k8s-for-beginners: crypto..z2fed25519..types: symbol not found
197Error relocating /k8s-for-beginners: crypto..z2fed25519..z2finternal..z2fedwards25519..types: symbol not found
198Error relocating /k8s-for-beginners: crypto..z2fhmac..types: symbol not found
199Error relocating /k8s-for-beginners: crypto..z2finternal..z2fsubtle..types: symbol not found
200Error relocating /k8s-for-beginners: crypto..z2frc4..types: symbol not found
201Error relocating /k8s-for-beginners: crypto..z2fsubtle..types: symbol not found
202Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcryptobyte..z2fasn1..types: symbol not found
203Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fhkdf..types: symbol not found
204Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2finternal..z2fsubtle..types: symbol not found
205Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fpoly1305..types: symbol not found
206Error relocating /k8s-for-beginners: hash..types: symbol not found
207Error relocating /k8s-for-beginners: internal..z2fbytealg..types: symbol not found
208Error relocating /k8s-for-beginners: internal..z2ffmtsort..types: symbol not found
209Error relocating /k8s-for-beginners: internal..z2fnettrace..types: symbol not found
210Error relocating /k8s-for-beginners: internal..z2frace..types: symbol not found
211Error relocating /k8s-for-beginners: internal..z2fsingleflight..types: symbol not found
212Error relocating /k8s-for-beginners: internal..z2fsyscall..z2fexecenv..types: symbol not found
213Error relocating /k8s-for-beginners: internal..z2fsyscall..z2funix..types: symbol not found
214Error relocating /k8s-for-beginners: math..types: symbol not found
215Error relocating /k8s-for-beginners: math..z2fbits..types: symbol not found
216Error relocating /k8s-for-beginners: net..z2fhttp..z2fhttptrace..types: symbol not found
217Error relocating /k8s-for-beginners: net..z2furl..types: symbol not found
218Error relocating /k8s-for-beginners: runtime..z2finternal..z2fatomic..types: symbol not found
219Error relocating /k8s-for-beginners: runtime..z2finternal..z2fmath..types: symbol not found
220Error relocating /k8s-for-beginners: strings..types: symbol not found
221Error relocating /k8s-for-beginners: sync..z2fatomic..types: symbol not found
222Error relocating /k8s-for-beginners: unicode..z2futf16..types: symbol not found
223Error relocating /k8s-for-beginners: unicode..z2futf8..types: symbol not found
224Error relocating /k8s-for-beginners: runtime.strequal..f: symbol not found
225Error relocating /k8s-for-beginners: runtime.memequal64..f: symbol not found
226Error relocating /k8s-for-beginners: type...1reflect.rtype: symbol not found
227Error relocating /k8s-for-beginners: reflect.rtype.Align: symbol not found
228Error relocating /k8s-for-beginners: reflect.rtype.Align: symbol not found
229Error relocating /k8s-for-beginners: reflect.rtype.AssignableTo: symbol not found
230Error relocating /k8s-for-beginners: reflect.rtype.AssignableTo: symbol not found
231Error relocating /k8s-for-beginners: reflect.rtype.Bits: symbol not found
232Error relocating /k8s-for-beginners: reflect.rtype.Bits: symbol not found
233Error relocating /k8s-for-beginners: reflect.rtype.ChanDir: symbol not found
234Error relocating /k8s-for-beginners: reflect.rtype.ChanDir: symbol not found
235Error relocating /k8s-for-beginners: reflect.rtype.Comparable: symbol not found
236Error relocating /k8s-for-beginners: reflect.rtype.Comparable: symbol not found
237Error relocating /k8s-for-beginners: reflect.rtype.ConvertibleTo: symbol not found
238Error relocating /k8s-for-beginners: reflect.rtype.ConvertibleTo: symbol not found
239Error relocating /k8s-for-beginners: reflect.rtype.Elem: symbol not found
240Error relocating /k8s-for-beginners: reflect.rtype.Elem: symbol not found
241Error relocating /k8s-for-beginners: reflect.rtype.Field: symbol not found
242Error relocating /k8s-for-beginners: reflect.rtype.Field: symbol not found
243Error relocating /k8s-for-beginners: reflect.rtype.FieldAlign: symbol not found
244Error relocating /k8s-for-beginners: reflect.rtype.FieldAlign: symbol not found
245Error relocating /k8s-for-beginners: reflect.rtype.FieldByIndex: symbol not found
246Error relocating /k8s-for-beginners: reflect.rtype.FieldByIndex: symbol not found
247Error relocating /k8s-for-beginners: reflect.rtype.FieldByName: symbol not found
248Error relocating /k8s-for-beginners: reflect.rtype.FieldByName: symbol not found
249Error relocating /k8s-for-beginners: reflect.rtype.FieldByNameFunc: symbol not found
250Error relocating /k8s-for-beginners: reflect.rtype.FieldByNameFunc: symbol not found
251Error relocating /k8s-for-beginners: reflect.rtype.Implements: symbol not found
252Error relocating /k8s-for-beginners: reflect.rtype.Implements: symbol not found
253Error relocating /k8s-for-beginners: reflect.rtype.In: symbol not found
254Error relocating /k8s-for-beginners: reflect.rtype.In: symbol not found
255Error relocating /k8s-for-beginners: reflect.rtype.IsVariadic: symbol not found
256Error relocating /k8s-for-beginners: reflect.rtype.IsVariadic: symbol not found
257Error relocating /k8s-for-beginners: reflect.rtype.Key: symbol not found
258Error relocating /k8s-for-beginners: reflect.rtype.Key: symbol not found
259Error relocating /k8s-for-beginners: reflect.rtype.Kind: symbol not found
260Error relocating /k8s-for-beginners: reflect.rtype.Kind: symbol not found
261Error relocating /k8s-for-beginners: reflect.rtype.Len: symbol not found
262Error relocating /k8s-for-beginners: reflect.rtype.Len: symbol not found
263Error relocating /k8s-for-beginners: reflect.rtype.Method: symbol not found
264Error relocating /k8s-for-beginners: reflect.rtype.Method: symbol not found
265Error relocating /k8s-for-beginners: reflect.rtype.MethodByName: symbol not found
266Error relocating /k8s-for-beginners: reflect.rtype.MethodByName: symbol not found
267Error relocating /k8s-for-beginners: reflect.rtype.Name: symbol not found
268Error relocating /k8s-for-beginners: reflect.rtype.Name: symbol not found
269Error relocating /k8s-for-beginners: reflect.rtype.NumField: symbol not found
270Error relocating /k8s-for-beginners: reflect.rtype.NumField: symbol not found
271Error relocating /k8s-for-beginners: reflect.rtype.NumIn: symbol not found
272Error relocating /k8s-for-beginners: reflect.rtype.NumIn: symbol not found
273Error relocating /k8s-for-beginners: reflect.rtype.NumMethod: symbol not found
274Error relocating /k8s-for-beginners: reflect.rtype.NumMethod: symbol not found
275Error relocating /k8s-for-beginners: reflect.rtype.NumOut: symbol not found
276Error relocating /k8s-for-beginners: reflect.rtype.NumOut: symbol not found
277Error relocating /k8s-for-beginners: reflect.rtype.Out: symbol not found
278Error relocating /k8s-for-beginners: reflect.rtype.Out: symbol not found
279Error relocating /k8s-for-beginners: reflect.rtype.PkgPath: symbol not found
280Error relocating /k8s-for-beginners: reflect.rtype.PkgPath: symbol not found
281Error relocating /k8s-for-beginners: reflect.rtype.Size: symbol not found
282Error relocating /k8s-for-beginners: reflect.rtype.Size: symbol not found
283Error relocating /k8s-for-beginners: reflect.rtype.String: symbol not found
284Error relocating /k8s-for-beginners: reflect.rtype.String: symbol not found
285Error relocating /k8s-for-beginners: reflect.rtype.common: symbol not found
286Error relocating /k8s-for-beginners: reflect.rtype.common: symbol not found
287Error relocating /k8s-for-beginners: reflect.rtype.rawString: symbol not found
288Error relocating /k8s-for-beginners: reflect.rtype.rawString: symbol not found
289Error relocating /k8s-for-beginners: reflect.rtype.uncommon..stub: symbol not found
290Error relocating /k8s-for-beginners: reflect.rtype.uncommon..stub: symbol not found
291Error relocating /k8s-for-beginners: reflect..reflect.rtype..d: symbol not found
292Error relocating /k8s-for-beginners: type...1net.IPAddr: symbol not found
293Error relocating /k8s-for-beginners: net.IPAddr.Network: symbol not found
294Error relocating /k8s-for-beginners: net.IPAddr.Network: symbol not found
295Error relocating /k8s-for-beginners: net.IPAddr.String: symbol not found
296Error relocating /k8s-for-beginners: net.IPAddr.String: symbol not found
297Error relocating /k8s-for-beginners: net.IPAddr.family: symbol not found
298Error relocating /k8s-for-beginners: net.IPAddr.family: symbol not found
299Error relocating /k8s-for-beginners: net.IPAddr.isWildcard: symbol not found
300Error relocating /k8s-for-beginners: net.IPAddr.isWildcard: symbol not found
301Error relocating /k8s-for-beginners: net.IPAddr.sockaddr: symbol not found
302Error relocating /k8s-for-beginners: net.IPAddr.sockaddr: symbol not found
303Error relocating /k8s-for-beginners: net.IPAddr.toLocal: symbol not found
304Error relocating /k8s-for-beginners: net.IPAddr.toLocal: symbol not found
305Error relocating /k8s-for-beginners: net.IPAddr..d: symbol not found
306Error relocating /k8s-for-beginners: runtime.main: symbol not found
307Error relocating /k8s-for-beginners: runtime_iscgo: symbol not found
308Error relocating /k8s-for-beginners: runtime_isstarted: symbol not found
309Error relocating /k8s-for-beginners: runtime_isarchive: symbol not found
310Error relocating /k8s-for-beginners: __gcc_personality_v0: symbol not found
311Error relocating /k8s-for-beginners: io.Writer..d: symbol not found
312Error relocating /k8s-for-beginners: runtime.writeBarrier: symbol not found
313
314FROM golang:1.17 as builder
315
316# first (build) stage
317
318WORKDIR /app
319COPY . .
320RUN go mod download
321RUN CGO_ENABLED=0 go build -o k8s-for-beginners
322
323# final (target) stage
324
325FROM alpine:3.10
326COPY --from=builder /app/k8s-for-beginners /
327CMD [&quot;/k8s-for-beginners&quot;]
328go mod init k8sapp     # creates a `go.mod`
329

or you can create go.mod manually:

1CONTAINER ID   IMAGE                      COMMAND                CREATED          STATUS                      PORTS     NAMES
2ae327a2bdba3   k8s-for-beginners:v0.0.1   &quot;/k8s-for-beginners&quot;   11 seconds ago   Exited (1) 10 seconds ago             focused_booth
3standard_init_linux.go:228: exec user process caused: no such file or directory
4FROM alpine:3.10
5COPY k8s-for-beginners /
6CMD [&quot;/k8s-for-beginners&quot;]
7package main
8
9import (
10    &quot;fmt&quot;
11    &quot;log&quot;
12    &quot;net/http&quot;
13)
14
15func main() {
16    http.HandleFunc(&quot;/&quot;, handler)
17    log.Fatal(http.ListenAndServe(&quot;0.0.0.0:8080&quot;, nil))
18}
19
20func handler(w http.ResponseWriter, r *http.Request) {
21    log.Printf(&quot;Ping from %s&quot;, r.RemoteAddr)
22    fmt.Fprintln(w, &quot;Hello Kubernetes Beginners!&quot;)
23}
24CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o k8s-for-beginners
25sudo docker build -t k8s-for-beginners:v0.0.1 .
26sudo docker run -p 8080:8080 -d k8s-for-beginners:v0.0.1
27        /lib64/ld-linux-x86-64.so.2 (0x7f9ab5778000)
28        libc.so.6 =&gt; /lib64/ld-linux-x86-64.so.2 (0x7f9ab5778000)
29Error loading shared library libgo.so.16: No such file or directory (needed by /k8s-for-beginners)
30Error loading shared library libgcc_s.so.1: No such file or directory (needed by /k8s-for-beginners)
31Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by /k8s-for-beginners)
32Error relocating /k8s-for-beginners: crypto..z2frsa..import: symbol not found
33Error relocating /k8s-for-beginners: crypto..z2fx509..import: symbol not found
34Error relocating /k8s-for-beginners: log..import: symbol not found
35Error relocating /k8s-for-beginners: crypto..z2fmd5..import: symbol not found
36Error relocating /k8s-for-beginners: crypto..import: symbol not found
37Error relocating /k8s-for-beginners: bytes..import: symbol not found
38Error relocating /k8s-for-beginners: fmt.Fprintln: symbol not found
39Error relocating /k8s-for-beginners: crypto..z2felliptic..import: symbol not found
40Error relocating /k8s-for-beginners: crypto..z2fx509..z2fpkix..import: symbol not found
41Error relocating /k8s-for-beginners: crypto..z2frand..import: symbol not found
42Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fchacha20poly1305..import: symbol not found
43Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcurve25519..import: symbol not found
44Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fidna..import: symbol not found
45Error relocating /k8s-for-beginners: internal..z2foserror..import: symbol not found
46Error relocating /k8s-for-beginners: crypto..z2fecdsa..import: symbol not found
47Error relocating /k8s-for-beginners: net..z2fhttp.HandleFunc: symbol not found
48Error relocating /k8s-for-beginners: io..import: symbol not found
49Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp2..z2fhpack..import: symbol not found
50Error relocating /k8s-for-beginners: crypto..z2fcipher..import: symbol not found
51Error relocating /k8s-for-beginners: log.Fatal: symbol not found
52Error relocating /k8s-for-beginners: math..z2fbig..import: symbol not found
53Error relocating /k8s-for-beginners: runtime..import: symbol not found
54Error relocating /k8s-for-beginners: net..z2fhttp..import: symbol not found
55Error relocating /k8s-for-beginners: hash..z2fcrc32..import: symbol not found
56Error relocating /k8s-for-beginners: net..z2fhttp.ListenAndServe: symbol not found
57Error relocating /k8s-for-beginners: context..import: symbol not found
58Error relocating /k8s-for-beginners: fmt..import: symbol not found
59Error relocating /k8s-for-beginners: crypto..z2ftls..import: symbol not found
60Error relocating /k8s-for-beginners: errors..import: symbol not found
61Error relocating /k8s-for-beginners: internal..z2ftestlog..import: symbol not found
62Error relocating /k8s-for-beginners: runtime.setIsCgo: symbol not found
63Error relocating /k8s-for-beginners: runtime_m: symbol not found
64Error relocating /k8s-for-beginners: encoding..z2fhex..import: symbol not found
65Error relocating /k8s-for-beginners: mime..import: symbol not found
66Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2funicode..z2fbidi..import: symbol not found
67Error relocating /k8s-for-beginners: internal..z2freflectlite..import: symbol not found
68Error relocating /k8s-for-beginners: compress..z2fgzip..import: symbol not found
69Error relocating /k8s-for-beginners: sync..import: symbol not found
70Error relocating /k8s-for-beginners: compress..z2fflate..import: symbol not found
71Error relocating /k8s-for-beginners: encoding..z2fbinary..import: symbol not found
72Error relocating /k8s-for-beginners: math..z2frand..import: symbol not found
73Error relocating /k8s-for-beginners: runtime_cpuinit: symbol not found
74Error relocating /k8s-for-beginners: internal..z2fpoll..import: symbol not found
75Error relocating /k8s-for-beginners: mime..z2fmultipart..import: symbol not found
76Error relocating /k8s-for-beginners: runtime.check: symbol not found
77Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcryptobyte..import: symbol not found
78Error relocating /k8s-for-beginners: crypto..z2fsha512..import: symbol not found
79Error relocating /k8s-for-beginners: runtime.registerTypeDescriptors: symbol not found
80Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fchacha20..import: symbol not found
81Error relocating /k8s-for-beginners: runtime.setmodinfo: symbol not found
82Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2ftransform..import: symbol not found
83Error relocating /k8s-for-beginners: time..import: symbol not found
84Error relocating /k8s-for-beginners: encoding..z2fbase64..import: symbol not found
85Error relocating /k8s-for-beginners: crypto..z2fsha256..import: symbol not found
86Error relocating /k8s-for-beginners: __go_go: symbol not found
87Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp..z2fhttpguts..import: symbol not found
88Error relocating /k8s-for-beginners: path..z2ffilepath..import: symbol not found
89Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2fsecure..z2fbidirule..import: symbol not found
90Error relocating /k8s-for-beginners: os..import: symbol not found
91Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp..z2fhttpproxy..import: symbol not found
92Error relocating /k8s-for-beginners: net..z2ftextproto..import: symbol not found
93Error relocating /k8s-for-beginners: encoding..z2fasn1..import: symbol not found
94Error relocating /k8s-for-beginners: runtime.requireitab: symbol not found
95Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fdns..z2fdnsmessage..import: symbol not found
96Error relocating /k8s-for-beginners: path..import: symbol not found
97Error relocating /k8s-for-beginners: io..z2fioutil..import: symbol not found
98Error relocating /k8s-for-beginners: sort..import: symbol not found
99Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2funicode..z2fnorm..import: symbol not found
100Error relocating /k8s-for-beginners: internal..z2fcpu..import: symbol not found
101Error relocating /k8s-for-beginners: runtime.ginit: symbol not found
102Error relocating /k8s-for-beginners: runtime.osinit: symbol not found
103Error relocating /k8s-for-beginners: runtime.schedinit: symbol not found
104Error relocating /k8s-for-beginners: bufio..import: symbol not found
105Error relocating /k8s-for-beginners: crypto..z2finternal..z2frandutil..import: symbol not found
106Error relocating /k8s-for-beginners: runtime_mstart: symbol not found
107Error relocating /k8s-for-beginners: net..import: symbol not found
108Error relocating /k8s-for-beginners: strconv..import: symbol not found
109Error relocating /k8s-for-beginners: runtime.args: symbol not found
110Error relocating /k8s-for-beginners: runtime..z2finternal..z2fsys..import: symbol not found
111Error relocating /k8s-for-beginners: runtime.newobject: symbol not found
112Error relocating /k8s-for-beginners: syscall..import: symbol not found
113Error relocating /k8s-for-beginners: unicode..import: symbol not found
114Error relocating /k8s-for-beginners: net..z2fhttp..z2finternal..import: symbol not found
115Error relocating /k8s-for-beginners: encoding..z2fpem..import: symbol not found
116Error relocating /k8s-for-beginners: _Unwind_Resume: symbol not found
117Error relocating /k8s-for-beginners: reflect..import: symbol not found
118Error relocating /k8s-for-beginners: mime..z2fquotedprintable..import: symbol not found
119Error relocating /k8s-for-beginners: log.Printf: symbol not found
120Error relocating /k8s-for-beginners: runtime.typedmemmove: symbol not found
121Error relocating /k8s-for-beginners: crypto..z2fdsa..import: symbol not found
122Error relocating /k8s-for-beginners: crypto..z2fsha1..import: symbol not found
123Error relocating /k8s-for-beginners: bufio..types: symbol not found
124Error relocating /k8s-for-beginners: bytes..types: symbol not found
125Error relocating /k8s-for-beginners: compress..z2fflate..types: symbol not found
126Error relocating /k8s-for-beginners: compress..z2fgzip..types: symbol not found
127Error relocating /k8s-for-beginners: context..types: symbol not found
128Error relocating /k8s-for-beginners: crypto..types: symbol not found
129Error relocating /k8s-for-beginners: crypto..z2fcipher..types: symbol not found
130Error relocating /k8s-for-beginners: crypto..z2fdsa..types: symbol not found
131Error relocating /k8s-for-beginners: crypto..z2fecdsa..types: symbol not found
132Error relocating /k8s-for-beginners: crypto..z2felliptic..types: symbol not found
133Error relocating /k8s-for-beginners: crypto..z2finternal..z2frandutil..types: symbol not found
134Error relocating /k8s-for-beginners: crypto..z2fmd5..types: symbol not found
135Error relocating /k8s-for-beginners: crypto..z2frand..types: symbol not found
136Error relocating /k8s-for-beginners: crypto..z2frsa..types: symbol not found
137Error relocating /k8s-for-beginners: crypto..z2fsha1..types: symbol not found
138Error relocating /k8s-for-beginners: crypto..z2fsha256..types: symbol not found
139Error relocating /k8s-for-beginners: crypto..z2fsha512..types: symbol not found
140Error relocating /k8s-for-beginners: crypto..z2ftls..types: symbol not found
141Error relocating /k8s-for-beginners: crypto..z2fx509..types: symbol not found
142Error relocating /k8s-for-beginners: crypto..z2fx509..z2fpkix..types: symbol not found
143Error relocating /k8s-for-beginners: encoding..z2fasn1..types: symbol not found
144Error relocating /k8s-for-beginners: encoding..z2fbase64..types: symbol not found
145Error relocating /k8s-for-beginners: encoding..z2fbinary..types: symbol not found
146Error relocating /k8s-for-beginners: encoding..z2fhex..types: symbol not found
147Error relocating /k8s-for-beginners: encoding..z2fpem..types: symbol not found
148Error relocating /k8s-for-beginners: errors..types: symbol not found
149Error relocating /k8s-for-beginners: fmt..types: symbol not found
150Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fchacha20..types: symbol not found
151Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fchacha20poly1305..types: symbol not found
152Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcryptobyte..types: symbol not found
153Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcurve25519..types: symbol not found
154Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fdns..z2fdnsmessage..types: symbol not found
155Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp..z2fhttpguts..types: symbol not found
156Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp..z2fhttpproxy..types: symbol not found
157Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fhttp2..z2fhpack..types: symbol not found
158Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fnet..z2fidna..types: symbol not found
159Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2fsecure..z2fbidirule..types: symbol not found
160Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2ftransform..types: symbol not found
161Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2funicode..z2fbidi..types: symbol not found
162Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2ftext..z2funicode..z2fnorm..types: symbol not found
163Error relocating /k8s-for-beginners: hash..z2fcrc32..types: symbol not found
164Error relocating /k8s-for-beginners: internal..z2fcpu..types: symbol not found
165Error relocating /k8s-for-beginners: internal..z2foserror..types: symbol not found
166Error relocating /k8s-for-beginners: internal..z2fpoll..types: symbol not found
167Error relocating /k8s-for-beginners: internal..z2freflectlite..types: symbol not found
168Error relocating /k8s-for-beginners: internal..z2ftestlog..types: symbol not found
169Error relocating /k8s-for-beginners: io..types: symbol not found
170Error relocating /k8s-for-beginners: io..z2fioutil..types: symbol not found
171Error relocating /k8s-for-beginners: log..types: symbol not found
172Error relocating /k8s-for-beginners: math..z2fbig..types: symbol not found
173Error relocating /k8s-for-beginners: math..z2frand..types: symbol not found
174Error relocating /k8s-for-beginners: mime..types: symbol not found
175Error relocating /k8s-for-beginners: mime..z2fmultipart..types: symbol not found
176Error relocating /k8s-for-beginners: mime..z2fquotedprintable..types: symbol not found
177Error relocating /k8s-for-beginners: net..types: symbol not found
178Error relocating /k8s-for-beginners: net..z2fhttp..types: symbol not found
179Error relocating /k8s-for-beginners: net..z2fhttp..z2finternal..types: symbol not found
180Error relocating /k8s-for-beginners: net..z2ftextproto..types: symbol not found
181Error relocating /k8s-for-beginners: os..types: symbol not found
182Error relocating /k8s-for-beginners: path..types: symbol not found
183Error relocating /k8s-for-beginners: path..z2ffilepath..types: symbol not found
184Error relocating /k8s-for-beginners: reflect..types: symbol not found
185Error relocating /k8s-for-beginners: runtime..types: symbol not found
186Error relocating /k8s-for-beginners: runtime..z2finternal..z2fsys..types: symbol not found
187Error relocating /k8s-for-beginners: sort..types: symbol not found
188Error relocating /k8s-for-beginners: strconv..types: symbol not found
189Error relocating /k8s-for-beginners: sync..types: symbol not found
190Error relocating /k8s-for-beginners: syscall..types: symbol not found
191Error relocating /k8s-for-beginners: time..types: symbol not found
192Error relocating /k8s-for-beginners: unicode..types: symbol not found
193Error relocating /k8s-for-beginners: container..z2flist..types: symbol not found
194Error relocating /k8s-for-beginners: crypto..z2faes..types: symbol not found
195Error relocating /k8s-for-beginners: crypto..z2fdes..types: symbol not found
196Error relocating /k8s-for-beginners: crypto..z2fed25519..types: symbol not found
197Error relocating /k8s-for-beginners: crypto..z2fed25519..z2finternal..z2fedwards25519..types: symbol not found
198Error relocating /k8s-for-beginners: crypto..z2fhmac..types: symbol not found
199Error relocating /k8s-for-beginners: crypto..z2finternal..z2fsubtle..types: symbol not found
200Error relocating /k8s-for-beginners: crypto..z2frc4..types: symbol not found
201Error relocating /k8s-for-beginners: crypto..z2fsubtle..types: symbol not found
202Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fcryptobyte..z2fasn1..types: symbol not found
203Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fhkdf..types: symbol not found
204Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2finternal..z2fsubtle..types: symbol not found
205Error relocating /k8s-for-beginners: golang.x2eorg..z2fx..z2fcrypto..z2fpoly1305..types: symbol not found
206Error relocating /k8s-for-beginners: hash..types: symbol not found
207Error relocating /k8s-for-beginners: internal..z2fbytealg..types: symbol not found
208Error relocating /k8s-for-beginners: internal..z2ffmtsort..types: symbol not found
209Error relocating /k8s-for-beginners: internal..z2fnettrace..types: symbol not found
210Error relocating /k8s-for-beginners: internal..z2frace..types: symbol not found
211Error relocating /k8s-for-beginners: internal..z2fsingleflight..types: symbol not found
212Error relocating /k8s-for-beginners: internal..z2fsyscall..z2fexecenv..types: symbol not found
213Error relocating /k8s-for-beginners: internal..z2fsyscall..z2funix..types: symbol not found
214Error relocating /k8s-for-beginners: math..types: symbol not found
215Error relocating /k8s-for-beginners: math..z2fbits..types: symbol not found
216Error relocating /k8s-for-beginners: net..z2fhttp..z2fhttptrace..types: symbol not found
217Error relocating /k8s-for-beginners: net..z2furl..types: symbol not found
218Error relocating /k8s-for-beginners: runtime..z2finternal..z2fatomic..types: symbol not found
219Error relocating /k8s-for-beginners: runtime..z2finternal..z2fmath..types: symbol not found
220Error relocating /k8s-for-beginners: strings..types: symbol not found
221Error relocating /k8s-for-beginners: sync..z2fatomic..types: symbol not found
222Error relocating /k8s-for-beginners: unicode..z2futf16..types: symbol not found
223Error relocating /k8s-for-beginners: unicode..z2futf8..types: symbol not found
224Error relocating /k8s-for-beginners: runtime.strequal..f: symbol not found
225Error relocating /k8s-for-beginners: runtime.memequal64..f: symbol not found
226Error relocating /k8s-for-beginners: type...1reflect.rtype: symbol not found
227Error relocating /k8s-for-beginners: reflect.rtype.Align: symbol not found
228Error relocating /k8s-for-beginners: reflect.rtype.Align: symbol not found
229Error relocating /k8s-for-beginners: reflect.rtype.AssignableTo: symbol not found
230Error relocating /k8s-for-beginners: reflect.rtype.AssignableTo: symbol not found
231Error relocating /k8s-for-beginners: reflect.rtype.Bits: symbol not found
232Error relocating /k8s-for-beginners: reflect.rtype.Bits: symbol not found
233Error relocating /k8s-for-beginners: reflect.rtype.ChanDir: symbol not found
234Error relocating /k8s-for-beginners: reflect.rtype.ChanDir: symbol not found
235Error relocating /k8s-for-beginners: reflect.rtype.Comparable: symbol not found
236Error relocating /k8s-for-beginners: reflect.rtype.Comparable: symbol not found
237Error relocating /k8s-for-beginners: reflect.rtype.ConvertibleTo: symbol not found
238Error relocating /k8s-for-beginners: reflect.rtype.ConvertibleTo: symbol not found
239Error relocating /k8s-for-beginners: reflect.rtype.Elem: symbol not found
240Error relocating /k8s-for-beginners: reflect.rtype.Elem: symbol not found
241Error relocating /k8s-for-beginners: reflect.rtype.Field: symbol not found
242Error relocating /k8s-for-beginners: reflect.rtype.Field: symbol not found
243Error relocating /k8s-for-beginners: reflect.rtype.FieldAlign: symbol not found
244Error relocating /k8s-for-beginners: reflect.rtype.FieldAlign: symbol not found
245Error relocating /k8s-for-beginners: reflect.rtype.FieldByIndex: symbol not found
246Error relocating /k8s-for-beginners: reflect.rtype.FieldByIndex: symbol not found
247Error relocating /k8s-for-beginners: reflect.rtype.FieldByName: symbol not found
248Error relocating /k8s-for-beginners: reflect.rtype.FieldByName: symbol not found
249Error relocating /k8s-for-beginners: reflect.rtype.FieldByNameFunc: symbol not found
250Error relocating /k8s-for-beginners: reflect.rtype.FieldByNameFunc: symbol not found
251Error relocating /k8s-for-beginners: reflect.rtype.Implements: symbol not found
252Error relocating /k8s-for-beginners: reflect.rtype.Implements: symbol not found
253Error relocating /k8s-for-beginners: reflect.rtype.In: symbol not found
254Error relocating /k8s-for-beginners: reflect.rtype.In: symbol not found
255Error relocating /k8s-for-beginners: reflect.rtype.IsVariadic: symbol not found
256Error relocating /k8s-for-beginners: reflect.rtype.IsVariadic: symbol not found
257Error relocating /k8s-for-beginners: reflect.rtype.Key: symbol not found
258Error relocating /k8s-for-beginners: reflect.rtype.Key: symbol not found
259Error relocating /k8s-for-beginners: reflect.rtype.Kind: symbol not found
260Error relocating /k8s-for-beginners: reflect.rtype.Kind: symbol not found
261Error relocating /k8s-for-beginners: reflect.rtype.Len: symbol not found
262Error relocating /k8s-for-beginners: reflect.rtype.Len: symbol not found
263Error relocating /k8s-for-beginners: reflect.rtype.Method: symbol not found
264Error relocating /k8s-for-beginners: reflect.rtype.Method: symbol not found
265Error relocating /k8s-for-beginners: reflect.rtype.MethodByName: symbol not found
266Error relocating /k8s-for-beginners: reflect.rtype.MethodByName: symbol not found
267Error relocating /k8s-for-beginners: reflect.rtype.Name: symbol not found
268Error relocating /k8s-for-beginners: reflect.rtype.Name: symbol not found
269Error relocating /k8s-for-beginners: reflect.rtype.NumField: symbol not found
270Error relocating /k8s-for-beginners: reflect.rtype.NumField: symbol not found
271Error relocating /k8s-for-beginners: reflect.rtype.NumIn: symbol not found
272Error relocating /k8s-for-beginners: reflect.rtype.NumIn: symbol not found
273Error relocating /k8s-for-beginners: reflect.rtype.NumMethod: symbol not found
274Error relocating /k8s-for-beginners: reflect.rtype.NumMethod: symbol not found
275Error relocating /k8s-for-beginners: reflect.rtype.NumOut: symbol not found
276Error relocating /k8s-for-beginners: reflect.rtype.NumOut: symbol not found
277Error relocating /k8s-for-beginners: reflect.rtype.Out: symbol not found
278Error relocating /k8s-for-beginners: reflect.rtype.Out: symbol not found
279Error relocating /k8s-for-beginners: reflect.rtype.PkgPath: symbol not found
280Error relocating /k8s-for-beginners: reflect.rtype.PkgPath: symbol not found
281Error relocating /k8s-for-beginners: reflect.rtype.Size: symbol not found
282Error relocating /k8s-for-beginners: reflect.rtype.Size: symbol not found
283Error relocating /k8s-for-beginners: reflect.rtype.String: symbol not found
284Error relocating /k8s-for-beginners: reflect.rtype.String: symbol not found
285Error relocating /k8s-for-beginners: reflect.rtype.common: symbol not found
286Error relocating /k8s-for-beginners: reflect.rtype.common: symbol not found
287Error relocating /k8s-for-beginners: reflect.rtype.rawString: symbol not found
288Error relocating /k8s-for-beginners: reflect.rtype.rawString: symbol not found
289Error relocating /k8s-for-beginners: reflect.rtype.uncommon..stub: symbol not found
290Error relocating /k8s-for-beginners: reflect.rtype.uncommon..stub: symbol not found
291Error relocating /k8s-for-beginners: reflect..reflect.rtype..d: symbol not found
292Error relocating /k8s-for-beginners: type...1net.IPAddr: symbol not found
293Error relocating /k8s-for-beginners: net.IPAddr.Network: symbol not found
294Error relocating /k8s-for-beginners: net.IPAddr.Network: symbol not found
295Error relocating /k8s-for-beginners: net.IPAddr.String: symbol not found
296Error relocating /k8s-for-beginners: net.IPAddr.String: symbol not found
297Error relocating /k8s-for-beginners: net.IPAddr.family: symbol not found
298Error relocating /k8s-for-beginners: net.IPAddr.family: symbol not found
299Error relocating /k8s-for-beginners: net.IPAddr.isWildcard: symbol not found
300Error relocating /k8s-for-beginners: net.IPAddr.isWildcard: symbol not found
301Error relocating /k8s-for-beginners: net.IPAddr.sockaddr: symbol not found
302Error relocating /k8s-for-beginners: net.IPAddr.sockaddr: symbol not found
303Error relocating /k8s-for-beginners: net.IPAddr.toLocal: symbol not found
304Error relocating /k8s-for-beginners: net.IPAddr.toLocal: symbol not found
305Error relocating /k8s-for-beginners: net.IPAddr..d: symbol not found
306Error relocating /k8s-for-beginners: runtime.main: symbol not found
307Error relocating /k8s-for-beginners: runtime_iscgo: symbol not found
308Error relocating /k8s-for-beginners: runtime_isstarted: symbol not found
309Error relocating /k8s-for-beginners: runtime_isarchive: symbol not found
310Error relocating /k8s-for-beginners: __gcc_personality_v0: symbol not found
311Error relocating /k8s-for-beginners: io.Writer..d: symbol not found
312Error relocating /k8s-for-beginners: runtime.writeBarrier: symbol not found
313
314FROM golang:1.17 as builder
315
316# first (build) stage
317
318WORKDIR /app
319COPY . .
320RUN go mod download
321RUN CGO_ENABLED=0 go build -o k8s-for-beginners
322
323# final (target) stage
324
325FROM alpine:3.10
326COPY --from=builder /app/k8s-for-beginners /
327CMD [&quot;/k8s-for-beginners&quot;]
328go mod init k8sapp     # creates a `go.mod`
329module k8sapp
330
331go 1.17
332

Source https://stackoverflow.com/questions/68881023

QUESTION

How to apply one signature test to multiple positionals

Asked 2022-Feb-03 at 16:01

I wrote some code in https://github.com/p6steve/raku-Physics-Measure that looks for a Measure type in each maths operation and hands off the work to non-standard methods that adjust Unit and Error aspects alongside returning the new value:

1multi infix:&lt;+&gt; ( Measure:D $left, Real:D $right ) is export {
2    my $result   = $left.clone;
3    my $argument = $right;
4    return $result.add-const( $argument );
5}
6multi infix:&lt;+&gt; ( Real:D $left, Measure:D $right ) is export {
7    my $result   = $right.clone;
8    my $argument = $left;
9    return $result.add-const( $argument );
10}
11multi infix:&lt;+&gt; ( Measure:D $left, Measure:D $right ) is export {
12    my ( $result, $argument ) = infix-prep( $left, $right );
13    return $result.add( $argument );
14}
15

This pattern is repeated 4 times for <[+-*/]> so it amounts to quite a lot of boilerplate; I'd like to reduce that a bit.

So, is there a more terse way to apply a single Measure|Real test in the signature to both Positionals in a way that the multi is triggered if both or one but not neither match and that the position is preserved for the intransigent operations <[-/]>?

I am not sure that getting to no multis is the most elegant - perhaps just compress the Real-Measure and Measure-Real to one?

ANSWER

Answered 2021-Dec-30 at 03:53

There are a few ways to approach this but what I'd probably do – and a generally useful pattern – is to use a subset to create a slightly over-inclusive multi and then redispatch the case you shouldn't have included. For the example you provided, that might look a bit like:

1multi infix:&lt;+&gt; ( Measure:D $left, Real:D $right ) is export {
2    my $result   = $left.clone;
3    my $argument = $right;
4    return $result.add-const( $argument );
5}
6multi infix:&lt;+&gt; ( Real:D $left, Measure:D $right ) is export {
7    my $result   = $right.clone;
8    my $argument = $left;
9    return $result.add-const( $argument );
10}
11multi infix:&lt;+&gt; ( Measure:D $left, Measure:D $right ) is export {
12    my ( $result, $argument ) = infix-prep( $left, $right );
13    return $result.add( $argument );
14}
15subset RealOrMeasure where Real | Measure;
16multi infix:&lt;+&gt; ( RealOrMeasure:D $left, RealOrMeasure:D $right )  {
17    given $left, $right {
18       when Real,    Real    { nextsame }
19       when Real,    Measure { $right.clone.add-const($left)  }
20       when Measure, Real    {  $left.clone.add-const($right) }
21       when Measure, Measure { my ($result, $argument) = infix-prep $left, $right;
22                               $result.add($argument)}}
23
24}
25

(Note: I haven't tested this code with Measure; let me know if it doesn't work. But the general idea should be workable.)

Source https://stackoverflow.com/questions/70525665

QUESTION

How to generate a Rank 5 matrix with entries Uniform?

Asked 2022-Feb-03 at 08:39

I want to generate a rank 5 100x600 matrix in numpy with all the entries sampled from np.random.uniform(0, 20), so that all the entries will be uniformly distributed between [0, 20). What will be the best way to do so in python?

I see there is an SVD-inspired way to do so here (https://math.stackexchange.com/questions/3567510/how-to-generate-a-rank-r-matrix-with-entries-uniform), but I am not sure how to code it up. I am looking for a working example of this SVD-inspired way to get uniformly distributed entries.

I have actually managed to code up a rank 5 100x100 matrix by vertically stacking five 20x100 rank 1 matrices, then shuffling the vertical indices. However, the resulting 100x100 matrix does not have uniformly distributed entries [0, 20).

Here is my code (my best attempt):

1import numpy as np
2def randomMatrix(m, n, p, q):
3    # creates an m x n matrix with lower bound p and upper bound q, randomly.
4    count = np.random.uniform(p, q, size=(m, n))
5    return count
6
7Qs = []
8my_rank = 5
9for i in range(my_rank):
10  L = randomMatrix(20, 1, 0, np.sqrt(20))
11  # L is tall
12  R = randomMatrix(1, 100, 0, np.sqrt(20)) 
13  # R is long
14  Q = np.outer(L, R)
15  Qs.append(Q)
16
17Q = np.vstack(Qs)
18#shuffle (preserves rank 5 [confirmed])
19np.random.shuffle(Q)
20
21

ANSWER

Answered 2022-Jan-24 at 15:05

Not a perfect solution, I must admit. But it's simple and comes pretty close.
I create 5 vectors that are gonna span the space of the matrix and create random linear combinations to fill the rest of the matrix. My initial thought was that a trivial solution will be to copy those vectors 20 times.
To improve that, I created linear combinations of them with weights drawn from a uniform distribution, but then the distribution of the entries in the matrix becomes normal because the weighted mean basically causes the central limit theorm to take effect.
A middle point between the trivial approach and the second approach that doesn't work is to use sets of weights that favor one of the vectors over the others. And you can generate these sorts of weight vectors by passing any vector through the softmax function with an appropriately high temperature parameter.
The distribution is almost uniform, but the vectors are still very close to the base vectors. You can play with the temperature parameter to find a sweet spot that suits your purpose.

1import numpy as np
2def randomMatrix(m, n, p, q):
3    # creates an m x n matrix with lower bound p and upper bound q, randomly.
4    count = np.random.uniform(p, q, size=(m, n))
5    return count
6
7Qs = []
8my_rank = 5
9for i in range(my_rank):
10  L = randomMatrix(20, 1, 0, np.sqrt(20))
11  # L is tall
12  R = randomMatrix(1, 100, 0, np.sqrt(20)) 
13  # R is long
14  Q = np.outer(L, R)
15  Qs.append(Q)
16
17Q = np.vstack(Qs)
18#shuffle (preserves rank 5 [confirmed])
19np.random.shuffle(Q)
20
21from scipy.stats import ortho_group
22from scipy.special import softmax
23import numpy as np
24from matplotlib import pyplot as plt
25N    = 100
26R    = 5
27low  = 0
28high = 20
29sm_temperature = 100
30
31p       = np.random.uniform(low, high, (1, R, N))
32weights = np.random.uniform(0, 1, (N-R, R, 1))
33weights = softmax(weights*sm_temperature, axis = 1)
34p_lc    = (weights*p).sum(1)
35
36rand_mat = np.concatenate([p[0], p_lc])
37
38plt.hist(rand_mat.flatten())
39

enter image description here

Source https://stackoverflow.com/questions/70750396

Community Discussions contain sources that include Stack Exchange Network

Tutorials and Learning Resources in Math

Tutorials and Learning Resources are not available at this moment for Math

Share this Page

share link

Get latest updates on Math